I want to run a command every other Tuesday at 05.15am. Is there a way to do this?
1 Answers
The cron schedule for running a job at 05:15 on Tuesdays is
15 5 * * 2
or use the day name (this may not be supported everywhere),
15 5 * * tue
The easiest way to get your command to run every second time this schedule triggers it is to let the cron job keep its state as a temporary file. If the file exists, it is removed, but the job does nothing else. If the file does not exist, the command is run, and then the job creates the file.
So your schedule could look like this:
15 5 * * 2 if [ -e /tmp/statefile ]; then rm -f /tmp/statefile; else cmd; touch /tmp/statefile; fi
Obviously (?) this would be cleaner if the business logic was encapsulated in a shell script that was triggered by the cron schedule:
15 5 * * 2 /some/path/cron-wrapper
The cron-wrapper
shell script could look something like this:
#!/bin/sh
statefile=/tmp/statefile
if [ -e "$statefile" ]; then
rm -f "$statefile"
exit
fi
cmd # the command we want to run
touch "$statefile"
Note that this does not take care to preserve the exit status of the command (cmd
in the above examples). If you need to do that to e.g. let cron inform you of a failed run, then ensure that your wrapper script exits with the exit status of the command:
#!/bin/sh
statefile=/tmp/statefile
if [ -e "$statefile" ]; then
rm -f "$statefile"
exit
fi
cmd # the command we want to run
err=$?
touch "$statefile"
exit "$err"
The exact logic that you want to use here depends on your requirements. Do you, for example, want to avoid creating the state file if the command fails? You may also want to create the state file elsewhere in a safe location that is unlikely to be cleared out by maintenance scripts and where it is unlikely to overwrite other data or be overwritten by other projects running on the same machine.
The other way to do this is by employing some form of date calculations, as described in the currently accepted answer to a similar question: Run a script via cron every other week

- 333,661
# m h dom mon dow command 15 05 */14 * 2 /bin/ls
but I'm not sure it is working, or not, because the */8 valid too. – K-attila- Mar 23 '23 at 10:5315 11 */14 * 4 /bin/echo "Hopp" >>/tmp/cronchk
15 11 /8 4 /bin/echo "HippHopp" >>/tmp/cronchk – K-attila- Mar 23 '23 at 10:56less /tmp/cronchk /tmp/cronchk: No such file or directory
Thank you @Kusalandra, again. – K-attila- Mar 23 '23 at 11:51