The 5 places cron jobs are saved
Cron has been popular for decades because it's easy to use and available everywhere. The downside to this flexibility is that cron job maintenance and debugging can be a real headache. In this guide we explore five different places you can find cron jobs on your server.
The user crontab file
In Linux, each user has their own crontab that can be used to schedule jobs that will be run as that user. You can view, update and delete a user crontab file using the
Viewing your user crontab
$ crontab -l 0 9 * * * /var/cronitor/bin/water-the-plants.py 0 7 * * 6 /var/cronitor/bin/take-out-the-garbage.py
Viewing another user's crontab
$ crontab -u tstark -l 0 0 * * * /jarvis/reboot-arc-reactor
If your server has a lot of users it could be challenging to find the right one. If you can see the job details in your cron logs, the username may be included. In the example below you can see the ubuntu user running the database backup script:
Aug 5 4:05:01 dev01 CRON\[2128\]: (ubuntu) CMD (/var/cronitor/database-backup.sh)
To update a user crontab, use
crontab -e. The cron files themselves should not be updated directly. Using
crontab -e will validate your changes and signal cron to reload your file.
The root user crontab
Like any other user, root has a user crontab. Essentially the same as any other user crontab, you are editing the root crontab when you run
sudo crontab -e.
Jobs scheduled in the root user crontab will be executed as root with all of its privileges.
If your system has
/etc/cron.monthly directories, these can be used to schedule jobs by copying or symlinking a script or command.
In distributions like Ubuntu, you can see when these scripts will be invoked by looking for lines in
$ head /etc/crontab 7 * * * * root cd / && run-parts — report /etc/cron.hourly 5 23 * * * root cd / && run-parts — report /etc/cron.daily 5 23 * * 7 root cd / && run-parts — report /etc/cron.weekly 5 23 1 * * root cd / && run-parts — report /etc/cron.monthly
System crontab file
The original and still commonly used way to schedule system-wide cron jobs is by adding entries to the crontab file
Writing to /etc/crontab requires admin privileges and jobs scheduled here can be run as any user. To specify the user, provide a username as the first word after the schedule expression. In this example, the job will be run by user dataproc:
$ tail /etc/crontab * 0 * * * dataproc /var/cronitor/bin/database-backup.sh
System drop-in directory /etc/cron.d
Another common way to schedule system-wide cron jobs is by adding a crontab file to
Like jobs scheduled in /etc/crontab, writing to /etc/cron.d requires elevated privileges and the first word after the expression of each cron job must specify which user will run the job.
Tip: For installers and scripts, adding a file to /etc/cron.d is a much cleaner option than adding entries to crontab files.
When individual user crontabs are edited using
crontab -e, the crontab files themselves are stored in
You can look, but avoid the temptation to edit crontab files here directly and always use
crontab -e. If you edit files in /var/spool/cron directly you will lose the benefit of syntax checking that the crontab editor provides, and your changes will not be detected without sending a reload signal to the cron daemon.