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.
Prerequisites
You need to have root privileges before you proceed further.
Installing Cron and Crontab
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
Opening crontab with a text editor
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
Syntax of the crontab
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
A basic example of a cron job
Here are some of the examples of cron jobs.
1. Schedule a cron job to execute at 2 AM daily
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.
2. Schedule a cron job to execute twice a 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.
3. Schedule a cron job to execute on every minute
* * * * * scripts/script.sh
The above cron job will be executed on every minute.
4. Schedule a cron job to execute on every Sunday at 5 PM
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.
5. Schedule a cron job to execute on every 10 minutes
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.
6. Schedule a cron job to execute on selected months
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.
7. Schedule a cron job to execute on selected days
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
8. Schedule multiple tasks in a single cron job
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
9. Schedule a cron job to run every 30 seconds
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
10. Schedule a cron job to execute twice on every Sunday and Monday
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.
Frequently Asked Questions
What is a Cron Job?
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.
How do I view existing Cron Jobs in Debian?
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.*.
How can I create a new Cron Job in Debian?
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.
What is the format of a Cron Job entry?
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.
How do I specify the time intervals for a Cron Job?
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).
Can I edit the system-wide Cron Jobs?
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.
Why is my Cron Job not running?
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.
Can I run a Cron Job at reboot?
Yes, you can schedule a Cron Job to run at reboot by using @reboot in the time field of the crontab entry.
How can I secure my Cron Jobs?
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.
Conclusion
Thank you for reading this article on cron jobs. Have a great day!!