Cron is a time-based scheduler to execute scripts/commands on a time frame that you set. This is a neat little utility that is often over-looked for forgotten. I’ll be honest I didn’t have much experience for cron for a few years so basically forgot all there is to know!
Well here is the basics anyone needs to know… cron has the concept of tabs, a tab is each “thing to do” on the time scheduled. For example, my use case recently was to renew my Lets Encrypt certificates (check them out here, https://letsencrypt.org/ #securethenet), they expire every 3 months and I always am bad at tracking so I decided to use a crontab to run the certbot renew command for me.
Will list out all of the existing cron tabs that you have deployed, it might show or complain if you’re not root; maybe sudo the command see if that changes output.
Now to create a crontab you run the below, this will use your editor you have selected (or need to select) to open the file where you can define 1->X tabs.
You may be wondering “how the hell do I know the time formatting“… well a website called cronguru has got you covered!
An example of a crontab definition is below, I used the nicely annotated options which are not always understood/accepted by all *nix distro’s so heads up. Below this runs each week on a Sunday which executes the shell script that contains a few commands to renew certs (it by nature ignores if cert renewal isn’t due so don’t worry I am not being a tit and hammering Lets Encrypt).
I hear you ask now well how do I verify it’s running, well cron by nature will output into the syslog log file under /var/log/syslog (or maybe messages if you are on a different distro), one way I check that cron is running is using the following command…
# the -n 5 is interval time in seconds, -c is interpret ANSI colours watch -n 5 -c grep CRON /var/log/syslog # example output Every 5.0s: grep CRON /var/log/syslog crowzfx: Sun Jul 25 13:59:49 2021 Jul 25 12:29:01 crowzfx CRON: (root) CMD (/root/renew_cert.sh )
Cron is that tool in your *nix toolbelt that you usually forget about but when you remember it can bring some powerfully automation and processing. Happy cron-ing!