Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I know how to do every minute, but how about every 10 seconds?

share|improve this question

migrated from stackoverflow.com Mar 11 '11 at 3:54

This question came from our site for professional and enthusiast programmers.

add comment

3 Answers

up vote 5 down vote accepted

You can't schedule the job every ten seconds, but I suppose you could schedule the job to run every minute, and sleep in a loop in 10s intervals. This would be predicated on your command being completed before the ten second interval expires, or you'll get overlap when the next command runs. This feels like a precarious solution, but if you can guarantee very short execution of the main command of the script, it would work.

#!/bin/bash
i=0

while [ $i -lt 6 ]; do
  /run/your/command &
  sleep 10
  i=$(( i + 1 ))
done
share|improve this answer
    
Hi Michael. You're putting 10 second gaps between executions of the command. Say the command takes 8 seconds, then you'd start the second execution after 18, the third after 36, fourth at 54 - and it would still be running when the cron job kicked off another copy. If you put an ampersand (&) after /run/your/command it will be executed in the background which avoids this problem. –  Tony Mar 10 '11 at 3:22
    
@Tony good catch. Edited. –  Michael Mar 10 '11 at 3:32
add comment

I had a similar task last week. My solutions was to multiply the standard cron entries to the desired frequency. My crontab looks like:

* * * * * /usr/local/bin/php /var/www/myscript.php
* * * * * sleep 10; /usr/local/bin/php /var/www/myscript.php
* * * * * sleep 20; /usr/local/bin/php /var/www/myscript.php
* * * * * sleep 30; /usr/local/bin/php /var/www/myscript.php
* * * * * sleep 40; /usr/local/bin/php /var/www/myscript.php
* * * * * sleep 50; /usr/local/bin/php /var/www/myscript.php

If you want to check results of myscript.php, e.g. for debugging, just append

&> /tmp/myscipt.log

to each line in the crontab above. Then stderr and stdout get redirected to the log file.

share|improve this answer
add comment

Take a look at this forum post where the solution is discussed :

http://www.linuxquestions.org/questions/programming-9/seting-cron-to-run-a-script-every-second-425623/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.