Open In App

How to Set Up Cron Jobs in Ubuntu

Last Updated : 30 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Setting up cron jobs in Ubuntu allows you to automate repetitive tasks such as backups, clearing logs, or maintaining system. This guide will walk you through the steps to configure cron jobs in Ubuntu, providing detailed instructions and practical examples. Whether you're a system administrator or a developer, understanding Ubuntu cron job setup can significantly enhance your productivity.

How-to-setup-cron-jobs-in-Ubuntu
How to Set Up Cron Jobs in Ubuntu

What is Cron Job

Cron Job is an schedule task Automator that can be used to execute at any specified period of time. It is managed by the cron service in Linux and are useful for automating routine tasks which can include system maintenance, backing up system, etc.

Why Use Cron Jobs in Ubuntu

There can be ample of reasons for using Cron Jobs in Ubuntu, some of the are:

How to Set Up Cron Jobs On Ubuntu

The following steps to be followed to set up a Cron Job in Ubuntu:

Step 1: Connect to the server and update the system

Before beginning with setting up crontab connect the server and update the system software to the latest version available. We can do this by using the below command

apt-get update && apt-get upgrade

Step 2: Check if the cron package is installed

To check if cron is installed, run the following command

dpkg -l cron 

If not, install the cron package on Ubuntu using the following command:

apt-get install cron

Step 3: Verify if the cron service is running

To check whether the cron service is running on the system, we can use the following command

systemctl status cron

Step 4: Configure cron job on Ubuntu

To set up cron jobs, one needs to modify the /etc/crontab file which can be done by only the root user. You can edit the crontab file with the following text editor.

Example:

nano /etc/crontab

Before we take an example of cron tab execution let's understand the common syntax of the crontab:

Syntax:

* * * * * /path/to/command arg1 arg2

OR

* * * * * /root/backup.sh

In this syntax:

  • First * stands for representing minutes [0-59].
  • Second * stands for representing hour[0-23].
  • Third * stands for representing day [0-31].
  • Fourth * stands for representing month[0-12].
  • Fifth * stands for representing a day of the week[0-7].

After all the steps for installation of the cron tab and understanding common syntax, let's execute a cron tab with a suitable example.

Example 1: Task Scheduling

If we want to schedule a backup on the first day of each month at 9 PM, the following command performs this operation.

crontab -e //install your cron job by running this command.

Append the following entry:

0 9 1 * * /path/to/script/backup-script.sh

Example 2: Repetitive Task

Set up and run PHP script as a cron job to run the script every day at 10 AM.

crontab -e //add cron job

Append the following entry:

0 10 * * * /path/to/myphpscript.php

The following options are available in crontab:

  • crontab -l : List all your cron jobs.
  • crontab -r : Delete the current cron jobs.

For more information about cron, one can check the manual pages using:

man cron
man crontab 

Troubleshooting Guide

There are chances when you encounter issue while performing any task automation using Cron Jobs and Cron Tabs in Ubuntu, here are a few key pointers that you need to keep in mind:

  • Always use absolute path
  • Ensure to define the environment variables carefully
  • Cross test your test scripts before executing them
  • Make sure to have permission, if not use chmod + x on scripts
  • You can also combine crone with logrotate for log management.
  • Encounter any issue wile running script? - Try checking logs :grep CRON /var/log/syslog.

Conclusion

Mastering Cron Jobs in Ubuntu allows any individual to automate their important tasks, perform regular system maintenance to ensure the smooth work flow. In short, whether you're scheduling backups, clearing logs or running any regular maintenance script, working with the Cron Jobs can be a real deal for you.


Next Article

Similar Reads