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.

Is it possible to run a cron job every 30 seconds without a sleep command?

share|improve this question
3  
What is your intention? Is cron the right tool to use for this? –  Manuel Faux Aug 2 '09 at 20:56
    
Good question. –  Preet Sangha Aug 2 '09 at 20:59

6 Answers 6

up vote 27 down vote accepted

If your task needs to run that frequently, cron is the wrong tool. Aside from the fact that it simply won't launch jobs that frequently, you also risk some serious problems if the job takes longer to run than the interval between launches. Rewrite your task to daemonize and run persistently, then launch it from cron if necessary (while making sure that it won't relaunch if it's already running).

share|improve this answer
3  
The part "you also risk some serious problems if the job takes longer to run than the interval between launches." is not true and if it was it would apply equally to jobs that run every 5 minutes, every hour, or every month for that matter. That problem has solutions (using a pidfile or whatever and checking whether the job is already running before running it). So the issue is that cron just won't allow that frequency, but it is incorrect to say that there is something intrinsically wrong in executing a task every less than a minute. –  matteo Nov 13 '12 at 21:30
1  
I meant if you don't use a pidfile. If your job runs every X minutes, and takes more than X minutes to finish, you'll end up with jobs stacking up. If your job is also limited by some sort of resource (CPU, network/disk bandwidth, etc), then running more at a time will make it take even longer to finish, and eventually your computer will turn into a thrashing mess. –  duskwuff Nov 14 '12 at 3:08
    
You can use run-one to ensure a program / even a PHP script isn't starting a duplicate instance. sudo apt-get install run-one and call it by run-one <normal command> –  kouton Jul 3 at 10:34

Cron is designed to wake up at every minute, so it is not possible to do it without some hacking, for example sleep like you mentioned.

share|improve this answer

Candidate for the most creative misuse of a Linux command:

nohup watch -n 30 --precise yourprog >/dev/null &

If yourprog consists of:

date +%M.%S.%N >> yourprog.out

then yourprog.out might look like:

50.51.857291267
51.21.840818353
51.51.840910204
52.21.840513307
52.51.842455224
53.21.841195858
53.51.841407587
54.21.840629676

indicating a pretty good level of precision.

share|improve this answer
3  
I'm giving +1 for pure cheek –  Matt Simmons Aug 16 '09 at 14:31

I'd have a couple of concerns:

(1) sometimes a system gets busy and cannot start things exactly on the 30 second point, it is then possible that at the same time you are running one job another job would pop and then you have 2 (or more) jobs doing the same thing. Depending on the script, there may be some significant interference here. Thus, coding in such a script should contain some code to insure that only one instance of the given scripting is running at the same time.

(2) The script could possibly have a lot of overhead, and consume more system resources than you might want. This is true if you are competing against a lot of other system activities.

Thus as one poster has put it, in this case I'd seriously consider putting in a daemon running with additional processes to ensure it remains running if its of critical importance to your operations.

share|improve this answer
* * * * * /path/to/program
* * * * * sleep 30; /path/to/program

Don't forget to write something into your program so that it exits if a previous instance is already running.

#!/bin/sh

if ln -s "pid=$$" /var/pid/myscript.pid; then
  trap "rm /var/pid/myscript.pid" 0 1 2 3 15
else
  echo "Already running, or stale lockfile." >&2
  exit 1
fi

Of course, this still leaves a very small opportunity for failure, so search google for a better solution applicable to your environment.

share|improve this answer

You can do this with third party software.

An option that has worked well for me is frequent-cron

It allows millisecond precision and it gives you the option to defer the next execution until the current one has exited..

share|improve this answer
1  
Frequent-cron has worked well for us too and powers many of our production systems. We haven't ever had an issue with it. –  Homer6 Oct 30 '13 at 21:01

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.