In this article, we’ll examine how to use the Crontab command to automatically schedule and run tasks in the background at regular intervals.
System administrators find it difficult to deal with routine tasks manually; however, in Linux and Unix-like operating systems, such tasks can be scheduled and run automatically in the background without human involvement using the cron daemon.
Cron daemon, which is used to run scheduled tasks from the command line or use online tools to generate cron jobs, can be used to automate tasks such as backing up your Linux system, scheduling updates, synchronizing your files, and many more.
Cron checks the countable tasks on the schedule every minute that it is awake. We can schedule these types of recurring tasks using a table called Crontab.
Advice: Every user has their own crontab they can use to add, remove, and modify tasks. Users are automatically given access to cron, but we can limit who can use it by adding a line to the /etc/cron.deny file.
Each command in a crontab file consists of six fields that are actually separated by a space or a tab. The first five fields indicate how long tasks will take to complete, and the final field is for commands.
- Hold values between 0-59 for one minute.
- Hour (reserve values from 0 to 23)
- Day of Month (retain values in the range of 1-31)
- The month of the year (hold values between 1 and 12 or January through December; you can also use the first three letters of each month’s name, such as Jan or Jun.)
- Weekday (use Sun-Sat or values between 0 and 6; you can also use the first three letters of each day’s name, such as Sun or Wed.)
- The command or script you want to schedule, as specified by its path.
1. List Crontab Entries
Using the crontab command’s -l option for the current user, you can list or manage tasks.
# crontab -l
00 10 * * * /bin/ls >/ls.txt
2. Edit Crontab Entries
Using the -e option as shown below will allow you to edit the crontab entry. Schedule jobs in VI editor by following the example below. Stop pressing the WQ keys to automatically save the setting after making the necessary changes.
# crontab -e
3. List Scheduled Cron Jobs
Use the options -u (User) and -l (List) to list the scheduled tasks for the specific user tecmint.
# crontab -u tecmint -l
no crontab for tecmint
Notably, only the root user has full access to view the crontab entries of other users. Regular users cannot see other users.
4. Eliminate Crontab Input
Attention: If the -r parameter is used, Crontab will automatically remove finished scheduled jobs. Before deleting a user’s crontab, use the -i option.
# crontab -r
5. Prompt Before Deleting Crontab
Crontab’s -i option will ask for user approval before deleting the user’s crontab.
crontab -i -r
- crontab: really delete root’s crontab?
6. Acceptable Special Characters (*, -, /,?, and #)
- All values in the field or any possible value are marked with an asterisk (*).
- Range definition with the hyphen (-).
- Every ten minutes, or an increment of range, is denoted by the slash (/) in the first field.
- The comma (,) is used to divide items.
7. The system’s cron schedule
The predefine cron directory can be used, as shown below, by system administrators.
8. Designate a specific time for a job.
The jobs listed below delete empty files and directories from /tmp every day at 12:30 am. For the crontab command to work, the user name must be specified. An example of a cron job being run by the root user is shown below.
# crontab -e
30 0 * * * root find /tmp -type f -empty -delete
9. Unique Strings in the Common Schedule
|@reboot||Upon restarting the system, command will be executed.|
|@daily||Whenever possible, use @midnight.|
|@yearly||annual; once. The keyword @annually is also a possibility.|
If you want to use the same command, you must replace five fields with keywords.
10. Multiple commands with two && signs
Commands 1 and 2 in the example below are executed every day.
# crontab -e
11. Remove the email notification option.
Cron by default mails the user account running the cronjob. Add a cron job to your system if you want to disable it, following the example below. All output from the cron job will be directed to /dev/null by using the >/dev/null 2>&1 option at the end of the file.
[root@tecmint ~]# crontab -e
* * * * * >/dev/null 2>&1
Task automation may enable us to carry out our duties more effectively, accurately, and efficiently. Entering the command “man crontab” in your terminal will take you to a page in the crontab manual where you can find more details.