Job Automation on Linux

Automation with cron

In any Operating system, it is possible to create a schedule commands or jobs to be executed periodically. This process, known as job scheduling.

For Centos or any other linux, this process is handled by the cron service or a daemon called crond, which can be used of schedule tasks (also called jobs).

Importance of job scheduling

The Importance of the job scheduling, like taking schedule updates and synchronization of files and many more.

The critical tasks taking backups, Which the clients usually wants to be taken in nights, can easily be performed without the intervention of the administrator by scheduling a cron job.

Package Required

To install Cron, you need to install the cronie package with Cron
To install the packages, use the yum command in the following form:

[root@ittroubleshooter ~]# yum install cronie -y

Important files related to cron

/etc/cron.deny is  access control file that is used to restrict the users from cron jobs.

/etc/cron.allow is access control file that is used to allow only users whose names are mentioned in this file to use cron jobs.(This file does not exist by default)

/var/spool/cron/ directory stored the user-defined crontabs.(Normal user don’t have the permission to execute this directory)

Crontab Format

.—————- minute (0 – 59)
|  .————- hour (0 – 23)
|  |  .———- day of month (1 – 31)
|  |  |  .——- month (1 – 12) OR jan,feb,mar,apr …
|  |  |  |  .—- day of week (0 – 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
|  |  |  |  |
*  *  *  *  *    <command to be executed>

Options                                           Explanation
* Is treated as wild card. Meaning any Possible value.
*/5 Is treated as ever 5 minutes,hours,days or months.
2,4,6 Treated as an OR, So if placed in the hours, this could mean at 2, 4 or 6’o-clock
7-16 Treats for any value between 7 and 16.

1.)  Command for check the cron jobs.

[root@ittroubleshooter ~]# crontab -l
no crontab for root
[root@ittroubleshooter ~]#

2. ) Command for Check the cron jobs of a particular user

[root@ittroubleshooter ~]# crontab -l -u nagios
no crontab for nagios
[root@ittroubleshooter ~]# crontab -lu nagios
no crontab for nagios
[root@ittroubleshooter ~]#

The syntax is,

crontab -l -u <user name>

3.)  Setting a job to display the current date for every two minute on present console.

  • To set the above job the steps are:
  • Check the console on which you are working by following command.
[root@ittroubleshooter ~]# tty
/dev/pts/0
[root@ittroubleshooter ~]#

Note: /dev/pts/0 is the console address

  • Schedule the task as shown below.
  • # crontab -e and enter the field as shown below and save it as in vim editor
*/2 * * * * date > /dev/pts/0
~
~
:wq
[root@ittroubleshooter ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@ittroubleshooter ~]#
  • Restart the cron service.
[root@ittroubleshooter ~]# systemctl restart crond.service
[root@ittroubleshooter ~]#

Wait for two minute and check whether time is displaying or not. Every minute time will be
displayed as below.

[root@ittroubleshooter ~]# Thu Sep 15 17:03:01 IST 2016
Thu Sep 15 17:05:01 IST 2016
Thu Sep 15 17:07:01 IST 2016

4.) Schedule a cron job to run the logdeletion script “deletionscript” on every night at “2:20 AM”.

Check the location of the script and also check whether it is having execute permission or not.

[root@ittroubleshooter logscript]# ls -l
total 4
-rwxr-xr-x 1 root root 1162 Aug 31 11:04 deletionscript
[root@ittroubleshooter logscript]# pwd
/root/logscript
[root@ittroubleshooter logscript]#

Apply the job in crontab and restart the service.

20 2 * * *  sh /root/logscript/deletionscript
~
~
:wq
[root@ittroubleshooter ~]# crontab -e
crontab: installing new crontab
[root@ittroubleshooter ~]#systemctl restart crond.service
[root@ittroubleshooter ~]#

5.) Schedule a cron job so that user “nagios”  run the backup script “Fullbackup.sh” on 2th, 9th,  16th, 23th, 30th September at 3:40 AM.

To set above task edit the crontab in following passion, and restart the service.

40      3       2,9,16,23,30    9   *   sh /home/nagios/Fullbackup.sh
~
~
:wq
[root@ittroubleshooter ~]# crontab -e -u nagios
crontab: installing new crontab
[root@ittroubleshooter ~]# systemctl restart crond.service
[root@ittroubleshooter ~]

6.) Schedule a cron job so that user “nagios”  run the incremental backup script “incrementalbackup.sh” on 3th to 8th, 10th to 15 th,  17th to 22th, 24th to 29th September at 3:40 AM.

This task is very much similar to the previous one but there is only a small change in format.

40      3       2,9,16,23,30    9   *   sh /home/nagios/Fullbackup.sh
40     3    3-8,10-15,17-22,24-29    *    sh /home/nagios/incrementalbackup.sh
~
:wq
[root@ittroubleshooter ~]# crontab -e -u nagios
crontab: installing new crontab
[root@ittroubleshooter ~]# systemctl restart crond.service
[root@ittroubleshooter ~]

Read Also How To Deny and Allow Any User To Use Cron Jobs

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz