Scheduling cron jobs on Linux in 2 ways.

Introduction

In the tutorial, we will be scheduling cron jobs on Linux. Cron is to run the script or command at a given date and time.

It is to schedule the backup, for logrotate, and delete files on a weekly basis. Suppose we have to take the backup or archive the logs daily then we can use cron jobs.

Cron jobs in Linux help us to automate the tasks without a fail. At the given timestamp it will run like a scheduler and do your mentioned job.

So there is a crond daemon which enables cron to run in the background. You can schedule the scripts at any time interval you want or as per the need.

There are 2 ways to schedule cron jobs as mentioned below,

  1. Scheduling cron jobs with a specific user.
  2. Scheduling cron jobs for the system.

Scheduling cron jobs with a specific user.

So create a bash script that will list the files and folders in the tastethelinux directory. And schedule this script to run every 5 minutes.

$ cat >> check_the_files.sh
#!/bin/bash
ls /home/tastethelinux/tastethelinux > /home/tastethelinux/tastethelinux/files_present.log

Once you have done with your script then press “cntrl+d” it will back to your terminal. Make the script in executable format by using the below command.

$ chmod +x check_the_files.sh

Now check the user you logged in with by using the “whoami” command.

$ whoami

OUTPUT:
tastethelinux

I have logged in with the user “tastethelinux”. So let’s edit the crontab file by executing the command “crontab -e” in a terminal.

$ crontab -e

OUTPUT:
no crontab for tastethelinux - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 2

So here we can see the option to select the editor, you can select the editor of your choice. I am going with vim.basic editor.

To “select an editor option” comes for the first time only. Once you select the editor then the next time, it will use the same editor to edit the crontab file.

*/5 * * * * /home/tastethelinux/check_the_files.sh

Once you have scheduled the script save and quit the file. let’s verify the scheduled cron jobs by using the “crontab -l” command.

$ crontab -l

OUTPUT:
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

*/5 * * * * /home/tastethelinux/check_the_files.sh

So you can see the scheduled job in the crontab file. It will run every 5 minutes and execute the script. Use “man crontab” to get detailed information or refer to crontab link.


Scheduling Cron jobs for the system in Linux.

To create cron jobs for the system we required root privileges. We have to edit the /etc/crontab file to schedule the script.

So create a bash script that will list the files and folders in the tastethelinux directory. And will schedule this script to run every 5 minutes.

$ cat >> check_the_files.sh
#!/bin/bash
ls /home/tastethelinux/tastethelinux > /home/tastethelinux/tastethelinux/files_present.log

Once you have done with your script then press “cntrl+d” it will back to your terminal. Make the script in executable format by using the below command.

$ chmod +x check_the_files.sh

So set up the job in the /etc/crontab file. You can use nano, vim, or vi editors to edit the crontab file.

$ sudo vim /etc/crontab
*/5 * * * *     root /home/tastethelinux/check_the_files.sh

So we have scheduled the job for every 5 minutes with the username root and path of the script. Now save and quit the file.

Check at every five-minute interval 5, 10, 15, and so on. In the “files_present.log” file, you will get the output at a 5-minute interval.

The log file is in the home directory inside the tastethelinux directory. In my case, the path is “(/home/tastethelinux/tastethelinux/files_present.log)”.

If anything goes wrong you can check the log files which are at /var/log/syslog. The logs file can be in a different path it depends upon the Linux distro you are using.


Explain cron jobs scheduling fields in Linux

fields for the crontab that we will use for cron jobs.

So on the above screenshot, we can see the fields like “* * * * *”. What this *(star) represents.

FieldValuesExamplesDescription
minute0 – 59*/5 * * * *Run job every 5th minute.
hour0 – 230 16 * * *Run job at 4:00 PM.
day of month1 – 310 16 25 * * *Run job at 25th day at 4:00 PM.
month1 – 120 0 * 5 *Run job at 25th May at 12:00 AM.
day of week0 – 6/1-70 3 * * 1Run job at 3 AM at Monday.

So there are 5 important fields that help us to schedule the cron job in Linux. let’s discuss this in detail.

  • minute: This field will execute the jobs at a particular minute. e.g: Run the jobs every 5 min.
  • hour: This field will execute the jobs on an hourly basis. e.g: Run the job every 6 hours.
  • day of month: Suppose you have to execute your job on a particular day of the month. e.g: Have to run the script on the 10th day of every month.
  • month: This field will execute the jobs on a monthly basis. e.g: You have to execute the job only in January.
  • day of week: This field will execute the job on a weekly basis. e.g: You have to run the script only from Monday to Friday.

Conclusion

So we have learned “Scheduling cron jobs with a specific user and for the system”. We have written a bash script and then schedule the jobs in the crontab file and in cron.

Explained 5 fields for cron jobs which helps us to schedule the cron jobs in many ways. 5 fields are minute, hour, day of month, month, and day of week.

Cron jobs help us to do automation for the manual tasks and the task that we have to perform very late at night. Find any issues or have to give us your valuable feedback then leave a comment.


Give your valuable time