Crontab is an important Linux tool for scheduling tasks so that programs and scripts can be executed at a specific time. In this article, I will teach you how to schedule a job in Debian 12 and show you some examples. The same commands will work on older Debian versions, too.
You need to have root privileges before you proceed further.
On my Debian system, cron was installed by default. However, if it is not installed on your machine, run the following few commands on the terminal with root privileges.
apt-get update
apt-get install cron
To get a list of cron jobs already scheduled on your machine, execute the following on terminal.
crontab -l
To open crontab with a text editor, execute the following command with root privileges.
crontab -e
As soon as the command is executed, you will be asked to choose the text editor as shown in the following screenshot.
The crontab -e command opens the crontab of the currently logged-in user, which is the root user in my example. To open the crontab of another user, let's say the user 'tom', add the -u flag followed by the name of the user. Example:
crontab -e -u tom
Linux crontab has six fields as shown below.
* * * * * /path/to/script.sh
Each of the fields has the following meaning.
[Minute] [hour] [Day_of_the_Month] [Month_of_the_Year] [Day_of_the_Week] [command]
Minute 0 - 59
Hour 0 - 23
Day of month 1 - 31
Month of year 1 - 12
Day of week 0 - 7
Here are some of the examples of cron jobs.
0 2 * * * /bin/sh backup.sh
The above cron job will be executed daily at 2 AM and will run a script backup.sh. This will maintain the backup every day.
0 5,17 * * * /scripts/script.sh
Above cron job will be executed at 5 AM and 5 PM daily. Multiple times can be specified with the help of comma.
* * * * * scripts/script.sh
The above cron job will be executed on every minute.
0 17 * * sun /scripts/script.sh
The above cron job will be executed on every Sunday at 5 PM. This type of cron is useful for doing weekly tasks like log rotation etc.
If you would like your job to execute every 10 minutes, cron needs to be updated as follows.
*/10 * * * * /scripts/monitor.sh
'*/10' means to run every 10 minutes.
Suppose you want to execute a cron in January, May, and August, the cron job needs to be set up as follows.
* * * jan,may,aug * /script/script.sh
Again multiple months can be specified by a comma.
If you would like your cron job to be executed on selected days suppose Sunday and Friday at 5 PM, it should look like the following.
0 17 * * sun,fri /script/script.sh
Multiple scripts can be run in a single task as follows. Both the scripts should be separated by a semicolon.
* * * * * /scripts/script.sh; /scripts/scrit2.sh
To schedule a cron job to execute every 30 seconds, we need to set up two cron's as follows:
* * * * * /scripts/script.sh * * * * * sleep 30; /scripts/script.sh
If you would like to schedule a job to execute twice at 4 AM and 5 PM every Sunday and Monday, the cronjob should look like this:
0 4,17 * * sun,mon /scripts/script.sh
Both, the hour and week of the day have been separated by commas.
A Cron Job in Debian is a scheduled task that runs automatically at specified intervals. It uses the Cron daemon to execute tasks that are defined in a crontab, a configuration file that specifies the schedule and command for each task.
To view existing Cron Jobs, use the command crontab -l. This will display the Cron Jobs for the current user. For system-wide Cron Jobs, check the /etc/crontab file and the directories inside /etc/cron.*.
To create a new Cron Job, use the crontab -e command. This opens the user's crontab file in the default text editor, where you can add a new line defining the schedule and command for the new task.
A Cron Job entry has five time-and-date fields, followed by the command to be executed. The format is: minute hour day-of-month month day-of-week command.
Time intervals in a Cron Job are specified using asterisks and numbers in the format: minute hour day month weekday. Each field can have a single number, a range, a list of numbers, or an asterisk (which represents "every" possible value for that field).
Yes, but it requires root privileges. Edit the /etc/crontab file or add scripts to the /etc/cron.daily, /etc/cron.weekly, or /etc/cron.monthly directories for tasks that run daily, weekly, or monthly, respectively.
<h4">How do I delete a Cron Job?
To delete a Cron Job, use crontab -e to edit the user's crontab file and simply remove the line corresponding to the job you want to delete. Save and exit the editor to apply the changes.
Common reasons include incorrect schedule format, permissions issues, environment variables not being set as in an interactive shell, or errors in the command itself. Check the syslog for error messages related to Cron.
Yes, you can schedule a Cron Job to run at reboot by using @reboot in the time field of the crontab entry.
Ensure that only trusted users have access to modify crontab files. Regularly review the Cron Jobs and their scripts for potential security issues. Keep the scripts used in Cron Jobs with minimal permissions necessary and owned by appropriate users.
Thank you for reading this article on cron jobs. Have a great day!!
Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…
ISPConfig is an open-source control panel that allows users to manage multiple servers from a…
As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…
Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…
Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…
phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…