The Linux OS is known for its automation and versatile utilities to automate tasks in every possible way. The system administrators often require automation to perform repetitive tasks with almost no user interaction. The “crontab” is the most efficient solution for the task automation on Debian and other Linux Distros.

The “cron” comes from the Greek word “Chronos”, which stands for “time”. The “crontab” works atop the crond daemon in the background. It reads the crontab for executing the scheduled scripts or the scheduled tasks. 

This guide explains everything about the “Debian crontab”.

Understanding the crontab.

Bonus Tips for Debian crontab

Final Words.

Let’s begin!

Understanding the crontab in Debian 12

The “crontab” allows the users to create a scheduled task which is automatically performed at the specified time. Below is the syntax for using the crontab on Debian 12. 

[Minute] [Hour] [Day (of the month)] [Month (of the year)] [Day (of the week)] [path_to_script_or_command]

All the crontab configurations are done via the “/tmp/crontab.random/crontab”. It creates a new file if the current user has no scheduled job.

crontab -e

Let’s explore more about the “crontab” via the following examples.

  • Example 1: Save the Date/Time Every Time  System Reboots.
  • Example 2: Run a Script.
  • Example 3: Run a Script After Every Five Minutes.
  • Example 4: Run a Script at Midnight.
  • Example 5: Run a Script on the First of Every Month.
  • Example 6: Run a Script Yearly.
  • Example 7: Run Multiple cronjobs.
  • Example 8: Run a Script From Monday to Friday After Every Hour Between 9-5.
  • Example 9: Sync a Remote Directory at a Specific Time
  • Example 10: Delete Temporary Files Every Day at Midnight.
  • Example 11: Monitor Disk Usage Every Hour.
  • Example 12: Back up a Directory at a Specific Time.

Example 1: Save the Date/Time Every Time System Reboots

You can save the date/time whenever your system reboots. To output the time to a specific file (date.txt in this case) every time after system reboots, add the following command after opening the “crontab” file:

@reboot date >> ~/date.txt

Here:

  • The “@reboot” tells the crontab what to do after the system reboots.
  • The “date >> ~/date.txt” outputs the current date and time to the “date.txt” file.

Additionally, you can make the “cronjob” wait (600 seconds in this case) using the following in the “crontab”:

@reboot && sleep 600 && /path/to/script.sh

Note: Save the changes in the “crontab” file using the “CTRL + S” and exit the file using the “CTRL + X” keys.

Example 2: Run a Script | Crontab

The “crontab” file can also execute the scripts at a specific time. To run a script in the crontab, append the path to the file by following the below format:

* * * * * /path-to-script

The above “cronjob” executes the “script.sh” and saves its results into the “test1.txt” file. Here, 

First Represents the minutes.
Second *Represents the hours (in 24-hour notation).
Third *Represents the day of the month.
Fourth *Represents the month of the year.
Fifth *Represents the day of the week.

Example 3: Run a Script After Every Five Minutes | Crontab

You can specify the time and make the “crontab” execute a specific script after the specified time. For instance, the below “cronjob” runs the command after every five minutes:

5 * * * * /home/user/script.sh

Example 4: Run a Script at Midnight | Crontab

To run a script at exactly 12 am or midnight, append the following in the “crontab” file:

0 0 * * * /home/user/script.sh

In the above “cronjob”:

  • The first 0 represents the 0 minutes.
  • The second 0 is the 0 hours or 12 am.

Note: You can use “@daily” or “@midnight” as well in the following format:

@daily or @midnight path-to-script

Example 5: Run a Script on the First of Every Month | Crontab

You can schedule a script to run on the first of every month using the following in the “crontab” file. It can be useful in automating salary distribution or other monthly tasks:

0 0 1 * * /home/user/script.sh

Note: You can use the “@monthly” as well to perform a specific task on the 1st of every month in the following way:

@monthly /home/user/script.sh

Example 6: Run a Script Yearly | Crontab

To schedule a task or script to run on January 1st of every new year at 12 am, use the following in the “crontab” file. You can use it to send New Year greetings messages or anything like that:

0 0 1 1 * /home/user/script.sh

Note: You can use the “@yearly” as well to perform a specific task on the 1st of every month in the following way:

@yearly /home/user/script.sh

Example 7: Run Multiple cronjobs | Crontab

You can specify multiple “cronjobs” for “crontab” using the following format:

* * * * * path-to-script1.sh; path-to-script2.sh

Example 8: Run a Script From Monday to Friday After Every Hour Between 9-5 | Crontab

You can run a script to remind your employees to report after every hour of their working time (9 am to 5 pm). To do it, append the below lines to the “crontab” file:

0 9-17 * * 1-5 /home/user/script.sh

Here, 

  • The “” stands for between the numbers (time). For instance “9-17” means 9 AM to 5 PM. While the “1-5” means the days (from Monday to Friday).

Example 9: Sync a Remote Directory at a Specific Time | Crontab

To automate the remote directory syncing process at a specific time, add the following in the “crontab” file. It syncs the local directory with a remote directory, but you must configure the “SSH” before you can use it:

0 1 * * * rsync -a /local/directory/ Remote-User@IP:/remote/directory/

Example 10: Delete Temporary Files Every Day at Midnight | Crontab

The temporary files on any OS including Debian 12 make the system load the program and their files faster. These files are often automatically deleted when the system reboots, but a few remain that can accumulate over time and consume disk space. To delete the temporary files every day at midnight, append:

0 1 * * * rm -rf /tmp/*

Example 11: Monitor Disk Usage Every Hour | Crontab

To schedule monitor the disk usage after every hour and save it in a file, append the following lines in the “crontab” file:

0 * * * * df -h >> ~/disk.txt

To view the output of the above “cronjob”, run the following command:

cat disk.txt

Example 12: Back up a Directory at a Specific Time | Crontab

To back up a directory at a specific time (Every Sunday between 4 AM and 6 AM in this case), use the below format in the “crontab” file:

0 4-6 * * 0 tar -czf /path/to/backup_$(date +%Y-%m-%d).tar.gz /path/to/directory

Bonus Tips for Debian crontab

The “crontab” can assist the users in the following ways:

List All Cronjobs

To list all cronjobs on Debian 12, run the following command:

crontab -l

List All cronjobs of Other Users

A root or privileged user who is in the sudoers group can view the “cronjobs” of other users (test in this case) using the below command:

sudo crontab -u test -l

Create cronjobs For Other Users

If you have the sudo privileges, you can create “cronjobs” for other users using the below-mentioned command:

crontab -u <username> -e

Remove the Active cronjobs

To remove the active “cronjobs” for the currently logged-in user, execute the following command:

crontab -r

Remove Active cronjobs for Other Users

With the sudo privileges, you can remove the active “cronjobs” for other users (test in this case) via the below command:

sudo crontab -u test -r

That’s all for “Debian crontab”.

Final Words

The “Debian crontab” lets you create the scheduled tasks that are automatically run at the specified time. You can use it to execute a script or a command at any time of the day, any day of the week, any month of the week, or yearly. In short, the “crontab” makes the automation on Linux even more simpler. This guide has shed light on the complete use of the “crontab” on Debian 12.