Thursday, 23 March 2017

How to schedule a job in Linux . How to create job automation in Linux.



In any operating system, it is possible to create jobs that you want to reoccur. This process, known as job scheduling, is usually done based on user-defined jobs. For Red hat or any other Linux, this process is handled by the cron service or a daemon called crond, which can be used to schedule tasks ( also called jobs).
The importance of the job scheduling is that the critical tasks like 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. If the cron job is scheduled carefully than the backup will be taken at any given time of the client and there will be no need for the administrator to remain back at night s to take the backup.

Important Files related to cron:-

/etc/crontab : is the file which stores all scheduled jobs
/etc/cron.deny : is the file used to restrict the users from using cron jobs
/etc/cron.allow : is used to allow only users whose names are mentioned in this file to use cron jobs. (this file does not exist by default)

Crontab format:

TO assign a job in the crontab file the format used is the following.


     *****         Command to be executed
    
1>    *  For Minute (0-59)
2>    *  For Hour (0-23)
3>    *  For Day of Month (1 -31 )
4>    *  For Month (1-12) OR jan,feb,mar,….
5>    *  For Day of Week (0-6) (Sunday= 0 or 7) OR sun,mon.tue,wed,thu,fri,sat

Corntab Commands:

Command
Explanation
Crontab –e
Edit your crontab file, or create one If it doesn’t already exist.
Crontab –l
Display your crontab file.
Crontab –r
Remove your crontab file.
Crontab –u
If combined with –e, edit a particular user’s Crontab file and if combined with –l, display a particular user’s crontab file. If combined with –r , deletes a particular user’s crontab file

How to check the assigned cron jobs of currently logged in user.

For Check The cron Jobs the command is
#crontab –l


[root@linuxelearn ~]# crontab -l
43 23 28 9 3 mkdir /root/rahull
[root@linuxelearn ~]#

How to check the cron jobs of a particular user.

To check a users cron jobs, the syntax is
#crontab –l –u <username>
#crontab –l –u user1


[root@linuxelearn ~]# crontab -l -u pooja
no crontab for pooja
[root@linuxelearn ~]# crontab -lu pooja
no crontab for pooja
[root@linuxelearn ~]#

How to set automation job to display the current date for every minute on present console.

TO set above job the steps are
Check the console on which you are currently working by following command
#tty


[root@linuxelearn ~]# tty
/dev/pts/1
[root@linuxelearn ~]#
Note: /dev/pts/1 is the console address

Schedule the job as show below
#crontab –e and enter the field as shown below and save it as in VI editor.


[root@linuxelearn ~]# crontab –e

*/1 * * * * date > /dev/pts/1
~
~
~
~
[root@linuxelearn ~]# crontab -e
crontab: installing new crontab
Note: Here * means every possible value

Restart the cron services using following command.
#service crond restart


[root@linuxelearn ~]# service crond restart
Stopping crond:                                            [  OK  ]
Starting crond:                                            [  OK  ]
[root@linuxelearn ~]#

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


[root@linuxelearn ~]#
[root@linuxelearn ~]# Sun Jan 22 22:41:01 IST 2017

How to remove a schedule job or task.

To remove a automation job use following command..

# crontab -r

[root@linuxelearn ~]# crontab -r
[root@linuxelearn ~]#
[root@linuxelearn ~]# crontab -l
no crontab for root
[root@linuxelearn ~]#

Using this method you can schedule more job in Linux…


No comments: