88 points · by todsacerdoti · 2y ago · 50 comments
The distinction between original Unix cron, anacron, and the at function.
What PATH applies.
Which POSIX are you on? Linux or BSD or SysV derived Unix differs in cron
Is cron even running by default? Containerised Unix may not do what you think
One syntax for /etc/cron.d (user to run as) and another for crontab -u <user> -e
The variation in which file it logs to in /var/log
Systemd and launchd and systemctl and launchctl and journalctl... (persisting daemons which run things according to the clock)
Daylight savings and ritualised habits about cronojbs at midnight
(Yes, I read the article. It's a great practical introduction to cron on most Linux situations people will find themselves in for the first time)
I have covered many of these topics here (see developer questions section)
* Do you know where it's going to send any output? If you're not certain then add a MAILTO entry at the start of the crontab to make it explicit
* Do your jobs require additional environment variables configuring?
* How noisy is your job's output? Any thing sent to STDOUT or STDERR will get emailed to either the recipients in the MAILTO option or the user that the crontab is for - initially you'll want a lot of output so you can check that it's working as expected, but once you're happy with it then you don't want all the information just any errors. There are many ways to achieve this like using --quiet options on commands to suppress output, or redirecting STDOUT to /dev/null or a file on disk (as errors should end up in STDERR).
Sep 6 02:20:01 ThinkPad-L560 CRON[3748665]: (root) CMD (/bin/bash /root/bin/backup.rsync /.git)
Sep 6 02:20:01 ThinkPad-L560 cron[3748668]: sendmail: fatal: open /etc/postfix/main.cf: No such file or directory
Sep 6 02:20:01 ThinkPad-L560 postfix/sendmail[3748668]: fatal: open /etc/postfix/main.cf: No such file or directory
Sep 6 02:20:01 ThinkPad-L560 CRON[3748664]: (root) MAIL (mailed 291 bytes of output but got status 0x004b from MTA#012)
...Yes, you need a systemd service file and a systemd timer file. Creating both is a matter of ~30m. Then, you do `systemctl start` and you're good... As a great benefit, you can see the status of each timer, which is great for debugging quick problems, like permission issues/runtime errors/... when setting the timer up.
I can write a cron entry in a minute or two, depending on the complexity.
Simeone just needs to write some CLI for making really simple timers.
That's a post-it or a simple-systemd-timers.txt though. Don't bother Simeone with that :).
while true; do
date;
./doit.sh;
sleep 300;
done | tee doit.log
if you want to get fancy, you can do: date "+%Y.%M.%dT%H:%m:%s-%Z`./doit.sh`"
to get it all on one line.There's really no reason to use cron today if you have systemd.
There’s a huge reason to use cron: it’s dead simple, doesn’t require a ton of complexity around it to function, and the syntax hasn’t changed in decades.
Everyone who does this enough knows it, everyone else [hopefully] rediscovers. Honest to goodness decades-long cargo culture.
I prefer not having this problem, using "systemd timers" instead. The job and any forked work have a reasonable default path, and observability is worlds better.
Did the last run succeed? When is the next? One command with timers, a guess with cron.
To where are you expecting to output to go? Does cron have permissions for that?
It also makes seeing the status of jobs and finding logs related to those jobs much easier.
Good overview is here: https://www.panozzaj.com/blog/2014/05/02/replace-local-cron-...
Sadly Jenkins is nowadays less and less popular and I'm concerned that it will become an abandon-ware at some point.
If anyone knows good open source alternative it would be great.
https://github.com/bdd/runitor
... which lets you quickly integrate any cron job with the https://healthchecks.io service. So darn handy. And you're supporting an indie startup that regularly shows up on HN.
Description (from its manpage): "Anacron can be used to execute commands periodically, with a frequency specified in days. Unlike cron(8), it does not assume that the machine is running continuously. Hence, it can be used on machines that aren't running 24 hours a day, to control daily, weekly, and monthly jobs that are usually controlled by cron."