How to Setup CronJob On A Linux Server

Setting up a cron job on Linux involves using the crontab command. Below is a step-by-step guide on how to set up a cron job:

  1. Open the Crontab Editor:
    To edit the current user’s crontab, use the following command:
   crontab -e
  1. Understand the Cron Time Format:
    Each cron job has a specific format:
   * * * * * /path/to/command

The five asterisks represent:

  • Minute (0 – 59)
  • Hour (0 – 23)
  • Day of the month (1 – 31)
  • Month (1 – 12)
  • Day of the week (0 – 7, where both 0 and 7 represent Sunday)
  1. Add Your Cron Job:
    For example, if you want to run /path/to/script.sh every day at 3:30 AM, you would add the following line to the crontab:
   30 3 * * * /path/to/script.sh
  1. Save and Exit:
    After adding your cron job, save and exit the editor. The method to save and exit depends on the default editor set on your system:
  • For vi/vim: Press Esc, type :wq, and press Enter.
  • For nano: Press CTRL + O, press Enter, and then press CTRL + X.
  1. View Current Cron Jobs:
    To view the current user’s cron jobs, use:
   crontab -l
  1. Remove a Cron Job:
    If you want to remove a cron job, edit the crontab (crontab -e) and simply delete the line containing the job you want to remove. Then save and exit.
  2. Manage Other User’s Cron Jobs:
    If you have superuser privileges, you can view or edit the crontab of another user with:
   crontab -u username -l   # To view
   crontab -u username -e   # To edit
  1. Cron Job Output:
    By default, the output of the cron job will be mailed to the user the job runs as. If you want to redirect the output to a file or discard it, you can use standard redirection:
   30 3 * * * /path/to/script.sh > /path/to/logfile.log 2>&1
  1. Ensure the Cron Daemon is Running:
    The cron jobs rely on the cron daemon to be running. You can check its status (on systems using systemd) with:
   systemctl status cron
  1. Set Environment Variables:
    If your script relies on specific environment variables, you can set them at the top of the crontab file. For example:
   PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
   30 3 * * * /path/to/script.sh

Make sure that the script or command you schedule with cron has the right permissions to run. Also, use absolute paths for any paths inside the script, since cron jobs have a limited environment.

Using Standard Redirection to output cron job

Let’s explore some examples of using redirection with cron jobs. This will help you understand how to handle the output and errors generated by cron tasks.

  1. Redirecting Standard Output to a File:
    If you have a cron job that generates output and you want to save that output to a file:
   0 0 * * * /path/to/script.sh > /path/to/output.log

This will run script.sh every day at midnight and save its output to output.log.

  1. Redirecting Errors to a Separate File:
    If you want to save error messages to a separate file:
   0 0 * * * /path/to/script.sh > /path/to/output.log 2> /path/to/error.log

This will save standard output to output.log and errors to error.log.

  1. Appending Output and Errors to Existing Files:
    If you don’t want to overwrite the log files but instead want to append to them:
   0 0 * * * /path/to/script.sh >> /path/to/output.log 2>> /path/to/error.log
  1. Combining Output and Errors in a Single File:
    If you want both the standard output and errors in the same file:
   0 0 * * * /path/to/script.sh > /path/to/output_and_error.log 2>&1
  1. Discarding Output and Errors:
    If you’re not interested in the output or errors of a cron job:
   0 0 * * * /path/to/script.sh > /dev/null 2>&1
  1. Sending Output and Errors via Email:
    By default, cron will email the user with the output and errors of a job. If you want to explicitly set an email address to receive these messages, you can use the MAILTO variable:
   MAILTO="your_email@example.com"
   0 0 * * * /path/to/script.sh
  1. Using Pipes with Cron Jobs:
    Let’s say you want to filter the output of a script and save only lines containing the word “ERROR” to a log file:
   0 0 * * * /path/to/script.sh | grep ERROR > /path/to/error.log
  1. Running Multiple Commands:
    If you want to run multiple commands in a single cron job and redirect their output:
   0 0 * * * /path/to/first_script.sh && /path/to/second_script.sh > /path/to/output.log

NOTE: when working with cron jobs, always use absolute paths for commands, scripts, and files to avoid any ambiguity. Also, test your scripts manually before scheduling them with cron to ensure they work as expected.

How to Check CronJob Logs

Execute the below command to get all the execution logs from CronJobs.

sudo grep CRON /var/log/syslog

Leave a Comment

Scroll to Top