Can I run a cron job more frequently than every minute?
Solution 1:
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 timewatch
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 forwatch
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 bywatch
to a file,/dev/null
. That file discards any data written to it. This prevents the output from being written to the screen or, sincenohup
is being used, it prevents output from being sent to a file callednohup.out
.&
- Thewatch
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 fordate
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 thedate
command to a file calledyourprog.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 2:
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 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.
Solution 4:
* * * * * /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.
Solution 5:
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..