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 the 5 different places you can find cron jobs on your server.

Instant alerts when your cron jobs fail

We created Cronitor after a critical cron job failed silently for months. Cronitor is easy to use and provides you with instant alerts when things go wrong. Learn more

  1. The user crontab

    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 crontab command.

    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

    Knowing where to look
    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. For example, here i 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)

    Tip:The user crontab is the easiest place to add a job so it’s commonly used and is the first place to look when you’re trying to locate a cron job.

    Tip: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.

  2. 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.

  3. A script or command in /etc/cron.hourly, /etc/cron.daily, etc

    If your system has /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /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 /etc/crontab:

    $ 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
  4. The system crontab file

    The original and still commonly used way to schedule system-wide cron jobs is by adding entries to the crontab file /etc/crontab.

    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
  5. A crontab file in the system drop-in directory /etc/cron.d

    Another common way to schedule system-wide cron jobs is by adding a crontab file to /etc/cron.d.

    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.

  6. Bonus: Understanding /var/spool/cron

    When individual user crontabs are edited using crontab -e, the crontab files themselves are stored in /var/spool/cron.

    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.

Our software, CronitorCLI, includes the free tools we always wished we had.  Scan your system and list every cron job in seconds using cronitor list.  Run any cron job instantly using cronitor select.