One of Linux five star things is the cronjobs. They allow you to automatically run stuf (scripts, applications, etc.) at quite specific times.
The crontab is the place controlling when the stuff is run and you can list the crontab using the commaand crontab -l for the current user.
If you want to edit the crontab, just use the command crontab -e and it launches the crontab in the default editor (set EDITOR env variable to change the editor used). The format is basically like this:
# Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * command to be executed 10 * * * * /script/to/run
All god an fine, if you want to run at specific times, but the cron can do quite powerful schedulling like run every other hour at night on weekdays during the summer - and much more.
The easiest way to setup the “five stars” is to use the website CrontabGuru. It will translate “crontab format” into English and easier allow you to make sure what will happen. CrontabGuru also allows you to se random examples to show you some of the powerful formatting the crontab supports.
Where might cronjobs also be hiding…
Apart from the login user cronjobs, you may also find some cronjobs hiding in /etc in these directories:
/etc/cron.d /etc/cron.daily /etc/cron.hourly /etc/cron.monthly
These are all directories and packages installed on the system may place files here as part of the installation to execute cronjobs. There’s also a system wide crontab at /etc/crontab.
Special crontab events
If you’re computer is running servers, home automation or other stuff one special crontab feature you might find helpful is the “magic words” which can be used in place of the five stars (setting up when the cronjob should run).
The crontab (on most systems) allows you to specify that a cronjob should run upon reboot by adding a line to the crontab like this:
Use cases might be for:
- Servers - plex, home automation, monitoring.
- Services - dropbox sync or monitoring services.
- A notification that power was lost and server has restarted.
- Check for updates and patches to the system (and potentialy applying them).
Beaware that the reboot cronjobs are run when the cron-agent is started, and that all system services may not be up and running at this point. A hack to fix this is often to prefix the command with “sleep 60 && /comannd/to/run.sh” as this will delay the job for a minute and allow services to launch if needed.
Apart from @reboot has more special words (@annually, @midnight, etc.). To see most of the powerful stuff you can do with cron, try the command man 5 crontab.
Listing all cronjobs
There doesn’t seem to be an easy way to list all cronjobs on a system, but one way to get an overview is by using this command line:
for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done
It will list all cronjobs for all users, but it will not show the jobs running in the /etc/crontab and the /etc/cron.* directories.