Note that there are some explanatory texts on larger screens.

plurals
  1. POC sleep variance when ran by cron
    text
    copied!<p>I wrote some C code to switch on and off some LEDs. Actually, I want to trigger them accurate to music but haven't found a better way than using <code>usleep()</code> in between yet.</p> <p>Anyway, turning on the LEDs, waiting at <code>usleep()</code> and turning off the LEDs again works pretty accurate when I call the program on the command line.</p> <p>Now I'd like cron to execute the program let's say every five minutes. Therefore I added following cronjob with <code>crontab -e</code>:</p> <pre><code>*/5 * * * * bash ~/startShow.sh &gt;~/log 2&gt;&amp;1 </code></pre> <p>Same problem without the shell script.</p> <pre><code>*/5 * * * * ~/projects/startLEDShow &gt;~/log 2&gt;&amp;1 </code></pre> <p>startShow.sh</p> <pre><code>date /usr/sbin/i2cset -y 1 0x40 0x00 0x21 # Preparation for communication via I2C cd projects ./startLEDShow </code></pre> <p>The program is triggered every five minutes, but at some point (not always the same point) the execution seems to stop for a moment and resumes about a second later. That is only when the program is ran by cron, not when I call it from the commandLine.</p> <p>Why is that so, is this perhaps due to the <code>usleep()</code> used in the program, and how can I make sure that the program execution isn't suspended for some time?</p> <p><strong>Update 1</strong>: Here is an extract with the usleep-section from the program</p> <pre><code>// Start reading while(NULL != (word = readToChar(fp, wordBuffer))) { // Values for the LEDs are stored in a struct updateValuesForLEDs(next, word); // Update struct "next" usleep(((next-&gt;time/timeFactor - lastTime)*1000000)); // Wait lastTime = next-&gt;time / timeFactor; // Set lastTime for next iteration setLEDs(i2cConnection, next, buffer); // Set the LEDs to the brightness values stored in struct "next" via I2C } </code></pre> <p><strong>Update 2:</strong> After ensc's comment I found way to solve my issue.</p> <p>The cronjob was started with a nice value of 10 whereas most other processes' values circled around 0. Setting my job to a lower nice value (which requires root privileges) gives it a higher priority and prevents it from being paused.</p> <pre><code>00 20 * * * sudo nice -n -20 ~/projects/startLEDShow &gt;~/log.txt 2&gt;&amp;1 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload