Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
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
up vote 31 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
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
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 '14 at 10:34
As answers further down show, it is very well possible, albeit a bit hackish. Why is YOU ARE DOING IT WRONG!!!! the accepted answer here, when it doesn't answer the question at all? – Someone May 23 '15 at 19:17
@Mantriur Because the question's author found it helpful and marked it as the accepted answer? :) But seriously, though, you've already identified the problem yourself: many of the other contemporary answers proposed hackish solutions which would be unwise to use in a production system. (Also, keep in mind that several of the other answers only showed up years after the question was asked, so they weren't available to accept yet!) – duskwuff May 23 '15 at 20:39

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:


indicating a pretty good level of precision.

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

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
* * * * * /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.


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

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
it has been asked: without a sleep command – philippe May 23 '15 at 21:40

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
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

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

A solution, if it's for your own script or if you can wrap it:

  1. Get and remember start time.
  2. If a lock file, which you will be touching later, is present and script has not been running for 60 seconds, wait a second and check again. (e.g. while/sleep)*
  3. If lock file is still present after 60 seconds have elapsed, exit with a stale lock warning.
  4. Touch lock file.
  5. While script has not been running for 60 seconds, loop your actual task with the desired sleep duration.
  6. Delete lock file.
  7. Add as minutely cron.
  8. Bob's your uncle.

Less of a headache than building and monitoring a daemon.

*If you're using PHP, remember clearstatcache().

share|improve this answer

Your Answer


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.