Ok so i have a cron that i need to run every 30 seconds...here is what i have below

*/30 * * * * /bin/bash -l -c 'cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\'''

It runs but is this 30 minutes or 30 seconds...and also I have been reading that cron might not be the best tool to use if I run it that often. Is there another better tool that i can install on ubuntu 11.04 that will be a better option or is there a way to fix the above cron

share|improve this question
    
CommaToast, and what happens if your Javascript or Java app falls over for some reason and exits? How will it restart? :-) – paxdiablo Feb 17 '15 at 1:24
9  
Add a little NodeJS app, lol. Why not a little c++ app? While we're at it, we can name it 'cron' and run it as a service. – Andrew Mar 20 '15 at 13:51
1  
possible duplicate of How to run Cronjobs more often than once per minute? – rubo77 Apr 23 '15 at 5:15

You have */30 in the minutes specifier - that means every minute but with a step of 30 (in other words, every half hour). Since cron does not go down to sub-minute resolutions, you will need to find another way.

One possibility, though it's a bit of a kludge, is to have two jobs, one offset by 30 seconds:

* * * * * /path/to/executable param1 param2
* * * * * ( sleep 30 ; /path/to/executable param1 param2 )

Both cron jobs actually run every minute but the latter one will wait half a minute before executing the "meat" of the job, /path/to/executable.

share|improve this answer
47  
if paxdiablo's answer helped you please mark it as the default answer. – sufinawaz Mar 26 '14 at 18:15
4  
This is a great workaround, so much so i think it transcends it's kludginess – Question Mark Jan 12 '15 at 10:25
    
wouldn't this work too? * * * * * ( /path/to/executable param1 param2; sleep 30 ; /path/to/executable param1 param2 ) – rubo77 Apr 23 '15 at 5:26
9  
@rubo77, only if it took less than a second to run :-) If it took 29 seconds, it would happen at 0:00:00, 0:00.59, 0:01:00, 0:01:59 and so on. – paxdiablo Apr 23 '15 at 5:30
1  
Thanks for the cool wordaround! – 尤川豪 May 8 '15 at 6:39

You can't. Cron has a 60 sec granularity.

* * * * * cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''
* * * * * sleep 30 && cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''
share|improve this answer

Cron's granularity is in minutes and was not designed to wake up every x seconds to run something. Run your repeating task within a loop and it should do what you need:

#!/bin/env bash
while [ true ]; do
 sleep 30
 # do what you need to here
done
share|improve this answer
42  
Keep in mind that isn't quite the same. If the job takes 25 seconds (for example), it will start every 55 seconds rather than every 30 seconds. It may not matter but you should be aware of the possible consequences. – paxdiablo Mar 8 '12 at 14:52
6  
You could run the job in background, then it will run in almost exactly 30 seconds. – Chris Koston Mar 22 '13 at 15:50
1  
while [true]do sleep 30 # do what you need to here done --------- done should be in small case – temple Nov 28 '13 at 21:02
1  
Won't the while [ true ] cause you to have lots of instances of the same script, since cron will start a new one every minute? – Carcamano Jan 23 '14 at 15:09
2  
You can do sleep $remainingTime where remainingTime is 30 minus the time the job took (and cap it at zero if it took > 30 seconds). So you take the time before and after the actual work, and calculate the difference. – mahemoff Sep 15 '14 at 10:29

No need for two cron entries, you can put it into one with:

* * * * * /bin/bash -l -c "/path/to/executable; sleep 30 ; /path/to/executable"

so in your case:

* * * * * /bin/bash -l -c "cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\'' ; sleep 30 ; cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''"

share|improve this answer
4  
Note: this only runs correct if the script takes less than a second to run – rubo77 Apr 23 '15 at 5:48
1  
Rubo - if the job is taking seconds long (instead of milli or microseconds) to complete, then you wouldn't run it every thirty seconds to be able to run twice per minute. So yes, start with 30 then subtract from that the approximate number of seconds per run, if greater than 1 second. – Andrew Apr 30 '15 at 15:51

You can check out my answer to this similar question

Basically, I've included there a bash script named "runEvery.sh" which you can run with cron every 1 minute and pass as arguments the real command you wish to run and the frequency in seconds in which you want to run it.

something like this

*/1 * * * * ~/bin/runEvery.sh 5 myScript.sh

share|improve this answer

Cron job cannot be used to schedule a job in seconds interval. i.e You cannot schedule a cron job to run every 5 seconds. The alternative is to write a shell script that uses sleep 5 command in it.

Create a shell script every-5-seconds.sh using bash while loop as shown below.

$ cat every-5-seconds.sh
#!/bin/bash
while true
do
 /home/ramesh/backup.sh
 sleep 5
done

Now, execute this shell script in the background using nohup as shown below. This will keep executing the script even after you logout from your session. This will execute your backup.sh shell script every 5 seconds.

$ nohup ./every-5-seconds.sh &
share|improve this answer
3  
The time will drift. For example, if backup.sh takes 1.5 seconds to run, it will execute every 6.5 seconds. There are ways to avoid that, for example sleep $((5 - $(date +%s) % 5)) – Keith Thompson Oct 15 '14 at 6:37
    
I am new to nohup, while executing your example, nohup is returning 'no such file or directory'. After some searches, you seems to missed out 'sh' after nohup. Like this: $ nohup sh ./every-5-seconds.sh & – VHanded Oct 23 '15 at 5:40

Use fcron (http://fcron.free.fr/) - gives you granularity in seconds and way better and more feature rich than cron (vixie-cron) and stable too. I used to make stupid things like having about 60 php scripts running on one machine in very stupid settings and it still did its job!

share|improve this answer
    
Confessions of a PHP developer ; ) – Eric Kigathi Feb 24 '17 at 17:04
    
Actually, confessions of a system engineer enabling PHP developers.... :) – Adi Chiru Nov 11 '17 at 17:35

Use watch:

$ watch --interval .30 script_to_run_every_30_sec.sh
share|improve this answer
    
can i use something like $ watch --interval .10 php some_file.php? or watch works only with .sh files ? – fonjeekay Jun 23 '17 at 10:38

Crontab job can be used to schedule a job in minutes/hours/days, but not in seconds. The alternative :

Create a script to execute every 30 seconds:

#!/bin/bash
# 30sec.sh

for COUNT in `seq 29` ; do
  cp /application/tmp/* /home/test
  sleep 30
done

Use crontab -e and a crontab to execute this script:

* * * * * /home/test/30sec.sh > /dev/null
share|improve this answer
    
if I understand this correctly, this script runs 30 times and waits 30 seconds in between every iteration. How does it make sense to run it every minute in cron? – FuzzyAmi Dec 26 '17 at 10:37

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.