5 ms·
Cron Jobs on Linux – Comprehensive Guide with Examples
- deleted 2y ago[deleted]
- ddejohn 2y agoI'm no expert but this seems like a pretty good crash course to me. One thing I still tend to use a lot when fiddling with cron jobs is crontab guru [1], which is very helpful for figuring out scheduling. [1] https://crontab.guru/ https://crontab.guru/
- hoherd 2y agoAnother great resource, especially when you have a lot of cron jobs, is https://cronheatmap.com https://cronheatmap.com which shows an hour-by-minute grid of one day, and how hot each of those minutes is for your given cron schedules.
- ggm 2y agoThe uncertainty of "did this edit to the crontab file get seen by the daemon". 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)
- blinded 2y agoNice overview. UX wise systemd timers are far superior.
- krupan 2y agoAre they though? Last time I played with them I had to create/edit two text files instead of one (service file and timer file), figure out if I needed to enable and start them both, figure out what to do if I want to temporarily disable the timer (systemctl stop? disable?), etc.
- pkulak 2y agoThat’s true. But once you get it set up, you can check the status, see when it last ran, when it will run next, the logs from the last run, if it failed last time, why, etc. You can stop it, start it, restart it, disable it. Simeone just needs to write some CLI for making really simple timers.
- johnchristopher 2y ago> 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 :).
- pkulak 2y agolol. I must have been on mobile.
- fragmede 2y agoOpen a terminal and then in bash: 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.
- zitsarethecure 2y ago
- chasil 2y agoI put all of my customization in the @reboot entry (usually for root).
- jftuga 2y agohttps://crontab.guru/ https://crontab.guru/ is a great web site to help you write schedules for your cron jobs.
- pixelmonkey 2y agoI have a soft spot for cron jobs. But if you use them, I highly recommend you also use the open source runitor tool: https://github.com/bdd/runitor https://github.com/bdd/runitor ... which lets you quickly integrate any cron job with the https://healthchecks.io https://healthchecks.io service. So darn handy. And you're supporting an indie startup that regularly shows up on HN.
- natmaka 2y ago'chronic' ("runs a command quietly unless it fails") is quite useful.
- WalterBright 2y agoEvery few years, I try to set up a cron job. I carefully follow the instructions. It never runs.
- mke1982 2y agoAlways explicitly set the path to whatever it is in your shell, it's usually something to do with this.
- bravetraveler 2y agoYep. Time honored tradition, every cron is edited at least twice. Once to put it there, again to fix PATH. 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.
- sgarland 2y agoLiterally the only issue I’ve had with cron was a bug [0] for an absurd edge case that I found and fixed. [0]: https://bugs-devel.debian.org/1019716 https://bugs-devel.debian.org/1019716
- bravetraveler 2y agoCongratulations, myself and several dozens of people I know/mentor have repeated the history I mentioned
- dotancohen 2y agoCan you post an example? To where are you expecting to output to go? Does cron have permissions for that?
- ndsipa_pomu 2y agoMost of the time, it's because cron jobs are using a different environment (i.e PATH and other variables) than you'd expect. What I sometimes do is to put a "screen -dm" command to run from cron (e.g. if you set it to run each minute and then remove it once it's run). Then, connect to the screen session and you can examine and test what's going on with the environment.
- pino82 2y agoNot everybody will like it (and nobody needs to use it), but: systemd timers do very similar things, are well integrated into the systemd ecosystem, have a nice syntax, allow all nice systemd features, and are quite nice imho.
- cocodill 2y agocron in 2024? could it be an anachronism?
- natmaka 2y agoThat's "anacron" job! :-) 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."
- alexpotato 2y agoPSA the Jenkins also has a builtin scheduler. 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-with-jenkins/ https://www.panozzaj.com/blog/2014/05/02/replace-local-cron-...
- sligor 2y agoYes ! that is what I do for personal use for years ! (mainly for backups) 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.
- alexpotato 2y agoThey are still actively committing to the project! https://github.com/jenkinsci/jenkins/graphs/commit-activity https://github.com/jenkinsci/jenkins/graphs/commit-activity
- scrapheap 2y agoUseful things to consider when using Cron: * 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).
- encoderer 2y agoCronitor solves the errors/output collection for you. Made by indie developers, launched here 10 years ago, nice free plan.
- everybodyknows 2y agoIs 'mail' broken in the default Ubuntu desktop install? Cron errors (if not redirecting output to a file) fail to deliver with: 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) ...
- brightball 2y agoI like setting up cron jobs with task spooler so they can run one after another and I don’t have to guess about potential overlap times.