Can I run a cron job more frequently than every minute?

Solution 1:

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

Solution 2:

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.

Here is an explanation of the parts of the command:

  • nohup - This keeps the command that follows it, watch in this case, from exiting when the terminal exits.
  • watch - This program runs a command repeatedly. Normally the first screenful of output from the command is displayed each time watch runs the command.
  • -n 30 - The interval at which to run the command. In this case it's every thirty seconds.
  • --precise - Without this option, watch runs the command after interval seconds. With it, each start of the command begins on the interval if possible. If this option were not specified in the example, the times would get later and later by more than 30 seconds each time due to the time it takes to launch and execute the command (yourprog).
  • yourprog - The program or command line for watch to execute. If the command line contains characters special to the shell (e.g. space or semicolon) it will need to be quoted.
  • >/dev/null - The greater-than redirects the output of the command being run by watch to a file, /dev/null. That file discards any data written to it. This prevents the output from being written to the screen or, since nohup is being used, it prevents output from being sent to a file called nohup.out.
  • & - The watch command is run in the background and control is returned to the terminal or parent process.

Note that nohup, the redirection of output and the & background control operator are not specific to watch.

Here is an explanation of the example yourprog script:

  • date - Outputs the current date and/or time. It can also set them.
  • +%M.%S.%N - This specifies the output format for date to use. %M is the current minute, %S is the current second and %N is the current nanosecond.
  • >> yourprog.out - This redirects the output of the date command to a file called yourprog.out. The double greater-than causes the output to be appended to the file on each invocation rather than the previous contents being overwritten.

Edit:

Possibly another thing that could be abused (or perhaps it's a legitimate use) is systemd timers.

See systemd/Timers as a cron replacement and Cron vs systemd timers.

I'll try to post an example soon.

Solution 3:

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.