The Cron software utility is a time-based job scheduler in Unix-like operating systems. Cron allows Linux and Unix users to run commands or scripts at a given time and date. Once can schedule scripts to be executed periodically. It is usually used for system admin jobs such as backups or cleaning/tmp/ directories and more.
The following steps to be followed to set up a cron job in Ubuntu:
- Connect to server and update the system:
Before begin with setting up crontab connect the server and update the system software to the latest version available. We can do this by using below command:
#apt-get update && #apt-get upgrade
- Check if cron package is installed:
To check if cron is installed, run the following command –
#dpkg -l cron
- If cron is not installed, install the cron package on Ubuntu:
One can Install the cron package with package Manager using the following command-
#apt-get install cron
- Verify if cron service is running:
To check whether the cron service is running on the system, we can 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/crontabfile which can be done by only root user. You can edit the crontab file with following text editor.
Before we take example of cron tab execution let’s understand the common syntax of cron tab:
* * * * * /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]. Fourth star stands for representing month[0-12]. Fifth * stand for representing day of the week[0-7].
After all step for installation of cron tab and understanding common syntax, let’s execute a cron tab with suitable example.
Example #1: If we want to schedule a backup on 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 cron job to run 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 the 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
- cron command in Linux with Examples
- Top 10 Highest Paying Tech Jobs
- Top 10 High Paying Jobs That Demand SQL
- How to install Ubuntu on VirtualBox?
- Keyboard Shortcuts for Ubuntu | Set - 1
- How to Install Ulauncher in Ubuntu?
- How to Install Chrome in Ubuntu?
- Interesting Facts about Ubuntu
- Get to know Ubuntu 18.04 LTS | Welcome Bionic Beaver!!
- How to Install php-curl in Ubuntu ?
- ES6 | Environment Setup
- How to Install and Configure Docker in Ubuntu?
- Installing and Configure vim Plugin in Ubuntu
- Snap Package Manager on Ubuntu
- 5 Ways to Keep Your Ubuntu System Clean
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Samdare B