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.

Pre-requisites:

  • Recommended OS: Ubuntu 20.04 Or Linux Mint 20
  • User account: A user account with sudo

This tutorial assumes that users already have installed the latest Linux operating system. To know further, refer to Linux Or Ubuntu based OS on your preference.

To schedule and list different cron jobs in Linux, you have to open the Terminal from Applications in your computer system.

Linux Terminal

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.

 Edit the crontab

 

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.

Example 1:

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.

crontab -e

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.

Example 2:

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.

 Schedule cronjob

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.

Method 1:

Enter the following command in the terminal, then hit enter.

$ crontab –l

List Cronjobs

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.

Edit root crontab

Method 2:

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.

$ /var/spool/cron/crontabs

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.

View Crontab using less command

Conclusion

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.

Share this page:

0 Comment(s)