Cron is a utility enabling Unix-like users to schedule and execute scripts or commands at specific times and dates.

Derived from the Greek word Chronos, meaning “Time,” Cron is particularly useful for scheduling repetitive tasks. These tasks include cleaning up temporary files, downloading files, and running backup scripts at regular intervals.

Each cron job is like one line of a crontab (i.e., cron table) file and it will run at scheduled intervals on a given schedule.

Cron is a daemon (a program that runs in the background) and needs to be started only once; it will be idle until it is required to be active. The cron daemon or crond will be idle until a time specified to run in the cron tabs.

Cron Job Components:

The majority of cron jobs include three components:

  • Script: The script can be called or executed.
  • Command: The command executes the script on the scheduled basis.
  • Action: The output or action of the script.

CronTab Commands For Quick Reference

  • crontab -l: Displays crontab file.
  • crontab -r: Removes crontab file.
  • crontab filename: Install filename as your crontab file.
  • crontab -e: Edit crontab file, or create one if it doesn’t already exist.
  • crontab -u user: This command allows you to modify or view the crontab file of the user. Only administrators can execute this option.

Cron Job Syntax

The syntax of a cron job is essential to understand. It consists of five fields, representing different time intervals, followed by the command to be executed.

* * * * * command-to-execute
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └───── day of week (0 - 7) (Sunday=0 or 7)
│ │ │ └────────── month (1 - 12)
│ │ └─────────────── day of month (1 - 31)
│ └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)

Practical Examples

Here are some common examples of cron jobs:

# Example: Run a backup script every day at 3am
0 3 * * * /path/to/backup/script.sh

# Example: Run a cleanup operation every Monday at 8pm
0 20 * * 1 /path/to/cleanup/script.sh

Common Use Cases

Cron jobs are commonly used for:

  • Database backups
  • Automated reports
  • System monitoring scripts
  • Automating maintenance tasks

Troubleshooting Tips

Here are some tips for troubleshooting cron jobs:

  • Ensure that the cron daemon is running.
  • Check the syntax of your cron job entries.
  • Verify the permissions and paths of the scripts you’re scheduling.
  • Review cron logs for errors or messages.

Frequently Asked Questions (FAQs) about Cron Jobs

What is a cron job?

A cron job is a scheduled task in Unix-like systems, used to automate repetitive tasks by running scripts or commands at specified times and dates.

How do I view my current cron jobs?

You can view your current cron jobs by using the command ‘crontab -l’ in the terminal.

How can I edit my crontab file?

To edit your crontab file, use the command ‘crontab -e’. This will open the file in your default text editor.

What is the syntax of a cron job?

A cron job has a specific syntax: minute hour day month weekday command, where each time interval is specified and followed by the command to execute.

How do I set a cron job to run every day at 2am?

To run a cron job every day at 2am, use ‘0 2 * * * command’, where ‘command’ is the script or command you want to run.

Can I set a cron job to run on specific days of the week?

Yes, you can specify particular days of the week in the fifth field of the cron job syntax, with 0 representing Sunday and 6 representing Saturday.

How can I remove a cron job?

To remove a specific cron job, use ‘crontab -e’ to edit the crontab file and delete the line containing the job. Save and exit the editor to apply changes.

Is it possible to log the output of a cron job?

Yes, you can redirect the output of a cron job to a file by appending ‘> logfile.txt’ to the end of the cron job line.

Can I set up a cron job to email me its output?

You can have cron email you the output of a job by setting the MAILTO variable at the beginning of your crontab file.

What should I do if my cron job isn't running?

If a cron job isn’t running, check its syntax, ensure the cron service is running, and verify that the script or command in the job has the correct permissions.

5/5 - (5 votes)

Pin It on Pinterest

Share This