Different ways to schedule and list CRON jobs in Linux
Cron is a built-in Linux utility used to run different processes within the computer system at a particular scheduled time.
Crontab i.e. the Cron Tables are used for reading the scripts that are predefined within a system and by using a syntax, users can do the configuration of a Cron job for scheduling the commands. Cron is a daemon for managing that enables you to manage task execution at prescribed time. These activities are referred to as cron jobs and may be scheduled to run by a minute, hour, month day, month, week day, or any combination thereof. If the task were to be carried out manually, this could be an incredibly difficult task for a user, since the user would have to be present every interval to perform the task. This tutorial will assist you through several techniques to schedule and list Cron jobs in Linux.
- Recommended OS: Ubuntu 20.04 Or Linux Mint 20
- User account: A user account with sudo
To schedule and list different cron jobs in Linux, you have to open the Terminal from Applications in your computer system.
Once the terminal window is open, check the logged-in user. To list the cron jobs,user must have sudo rights i.e. they must be an admin user. If the logged-in user already has admin rights, then proceed. Otherwise, switch to root user by executing the appended command.
$ su – username
Note: you can replace username with the name of root user of your OS.
Scheduling a Job in Crontab
The admin user/ root user can access the files in crontab that are scheduled.
To open the crontab file enter the below affixed command on the Linux terminal.
$ crontab -e
By hitting <Enter> key, it will open the user account’s crontab file.
Note: If your Linux system does not support the command sudo crontab -e, then try using the following mention command
$ su -c crontab -e
As you can notice, the opened editor is Nano. If you are provided with different choices of editors then select Nano since it is considered to be an easy choice.
Users can edit and schedule the cron jobs by using the arrow keys. Scroll down to the bottom of the file opened earlier within the editor, the lines started with # having comment will be completely ignored by cron editor.
To add the values for scheduling a job on a particular time, we will add the following values:
- Minutes of an hour (m)
- Hour of the day (h)
- Day of month (dom)
- Month of the year (mon)
- Day of week (dow)
Then you need to specify the jobs. For example, the following command will help in creating a backup at the defined time.
12 12 3 11 2 /root/backup.sh
In case if you want to skip any value, simply add an asterisk * in its place.
To save the file, use Ctrl-O and hit Enter. Select Y to confirm your selection. To exit, you can use the Ctrl-X shortcut once the file will be saved.
Now once you go back, the following message will appear, showing that new cron job is installed.
The entered information will tell the system to run Cron Job on which day, date and time. The scheduled job will then automatically run on the pre-set time by the user.
Another example of creating a cron job for setting computer hardware clock at 5:03 AM daily, you have to write the following code:
03 05 * * * /sbin/hwclock –systohc
Following the same steps, type the following command in the terminal as appended below and hit enter.
$ crontab –e
The opened file can be edited. So, add the code line mentioned above to schedule a new cron job.
To save the file, use Ctrl-O and hit Enter. Select Y to confirm your selection. To exit, use the Ctrl-X shortcut once the file will be saved. Our system’s hardware clock will be set as per our mentioned requirements. A success note will display while returning back.
Users can manage multiple tasks and automate them based on the system’s requirement.
Viewing the Cron jobs
The automated and scheduled tasks can also be viewed by the using the command line. Once the users have scheduled the jobs, they can access these jobs using the command.
Enter the following command in the terminal, then hit enter.
$ crontab –l
You can also use sudo crontab -l for the root user. Then click enter. It will display a list of scheduled cron jobs for the logged in user.
Another way to access these cron jobs is via directory. The cron jobs are located in the spool directories where they are further stored in crontabs. Users can discover them at the subsequent place in their machines. Go to the location mentioned below to access the cron jobs.
Root users have the privilege to use the crontab for the entire system. For displaying the content for root user’s crontab, we can use the following less command:
$ less /etc/crontab
On clicking enter, user can see the following sample output containing all cron jobs for root user in var/spool/cron directory.
This way users can navigate through the cron jobs on their Linux systems. Cron is useful for scheduling tasks that could be predefined and automated based on the requirements. To sort and show the tasks scheduled via the cron tool, use the commands from this guide. The crontab is directed by a crontab file, a configuration file that displays shell commands to be executed regularly for a particular schedule. By going through this article you can imagine how system administrators use it to automate backup activities, directory cleaning, alerts, etc.