The new version of ubuntu 21.04 is related and in this guide, you will learn how to configure setup cron in ubuntu 21.05. Cron job in ubuntu 21.04 is very simple.
The following steps to be followed to set up a cron job in Ubuntu:
Connect to the server and update the system:
run this command:
apt-get update apt-get upgrade
Configure the cron job. When you are logged in as your user, you are creating a cron job under that user. Creating a cron jobs owner is helpful when to know who is in charge of the cron as well as how to alter the cron job in the future.
The system asks which editor you’d like to use; this tutorial is using option 2 (vim.basic).
[email protected]:~$ crontab -e
no crontab for tom - using an empty one
Select an editor. To change later, run 'select-editor'.
Choose 1-3 : 2
In this file, you’ll see # signs followed by the direction on how to use the file. Allow us a minute to explain the syntax needed to create a cron task.
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
Check if the cron package is installed:
Turn the following command –
dpkg -l cron
If cron is not installed, install the cron package on Ubuntu:
using the following command-#apt-get install cron
Verify if cron service is running:
use the following command
systemctl status cron
Configure cron job on ubuntu:
In order 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.
Before we take the example of crontab execution let’s understand the common syntax of crontab:
* * * * * /path/to/command arg1 arg2 OR * * * * * /root/backup.sh
In the syntax first * stand for representing minutes [0-59]. Second * stands for representing hour[0-23]. Third * stand for representing day [0-31]. The fourth star stands for representing month[0-12]. The fifth * stand for representing the day of the week[0-7].
After all steps for the installation of crontab and understanding common syntax, let’s execute a crontab with a suitable example.
Example #1: 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: Set up and run PHP script as a cron job to run the script every day at
#crontab -e //add cron job // Append the following entry. 0 10 * * * /path/to/myphpscript.php
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