Install Crontab on Ubuntu: A Quick Guide
It is very easy to install and use the cron service and crontab in Ubuntu. Simply follow these steps:
- Install cron service:
Run the following command in the terminal to install the cron service: - Update the system with ‘sudo apt-get update’ and install the cron software with ‘sudo apt-get install cron’.
- Start cron service:
Run the following command in the terminal to start the cron service: - Initiate the cron service.
- Edit the crontab file:
Run the following command in the terminal to edit the crontab file for the current user: - edit crontab
- Add a cron job:
In the open crontab file, each line represents a cron job. The format of each line is as follows: - Execute the command located at “/path/to/command” with arguments “arg1” and “arg2”.
- * * * * * is a cron expression used to specify the execution time of a task. There are five fields representing minutes, hours, date, month, and day of the week. Each field can be a specific number, a range (e.g. 1-5), a comma-separated list (e.g. 1,3,5), or an asterisk (which means any value).
/path/to/command is the path to the command to be executed.
arg1, arg2 are optional command parameters. - Save and exit the crontab file.
- Restart the cron service:
Run the following command in the terminal to apply the changes: - Restart the cron service using sudo.
You have now successfully installed and used crontab to set up scheduled tasks. Keep in mind that only users with sudo privileges can edit the system-level crontab file. If you only want to set up cron tasks for the current user, use the crontab -e command to open the current user’s crontab file.