Scheduling Tasks On Linux | How To Schedule Tasks On Linux

Introduction:

Schedule tasks to automatically execute in the future. there are two basic daemons for scheduling tasks on a Linux System “at” command which is very useful for scheduling one time tasks and “crontab” are the routing tasks.

AT command:

The at command schedules a command to be run once at a particular time. It reads commands from standard input or script or file which can be executed later once. The at command can’t be used for any recurring tasks. For recurring tasks Linux crontab is used. For normal users, permission to use at command is determined by the files /etc/at.allow and /etc/at.deny . If the file /etc/at.allow exists, only usernames mentioned in tit are allowed to use at. If /etc/at.allow does not exist, /etc/at.deny is checked, every user name not mentioned in it is then allowed to user at. If neither exists, only the superuser is allowed use of at command.

Run AT command:

To start at command run at the command line, passing it the scheduled time as an option. It will place you at a special prompt, type the command or series of commands to be run at the scheduled time. When done, press control-D on a new line, and the command will be placed in the queue.

Install at package
[root@asim ~]#yum install at
Start at command service
[root@asim ~]#systemctl starat atd
Enable at package service on boot
[root@asim ~]#systemctl enable atd
At command passing scheduled time Passing touch command to create file Passing another command to save in file Press ctrl + D to terminate at command  
[root@asim ~]#at 5:20pm May 5
At>touch f1
At>date > f1
At>Ctrl +D
Just after 3 minutes
[root@asim ~]#at now + 3 minutes
Just after 1 hour
[root@asim ~]#at now + 1 hour
Just after 1 week
[root@asim ~]#at now + 1 week
just after 2 weeks
[root@asim ~]#at now + 2 week
View currently-queued at jobs
[root@asim ~]#atq
List each of the scheduled jobs
[root@asim ~]#at -l
List job contents
[root@asim ~]#at =c 1
Remove the at job number 1
[root@asim ~]#atrm 1 or at –r1

Crontab command:

Linux crontab is similar to windows task schedules. Crontab are very useful for routin tasks like scheduling system scanning, daily backup etc. Crontab executes jobs automatically in backend on specified time interval. For scheduling one time tasks you can use at command in Linux. Cron is a scheduler that can run commands at regular intervals. It’s often referred to as crontab which is name of its configuration file and the tool used to edit the configuration file. Crontab file consists of command per line and have six fields actually and separated either of space or tab. The beginning five fiels represent time to run tasks and last field is for command.

[Minute][Hour][Day_of_the_Month][Month_of_the_year][Day_Of_the_Week][Command]

Install cron package
[root@asim ~]#yum install cronie
Start cron service
[root@asim ~]#systemctl start crond
Check whether the crond service is running
[root@asim ~]#systemctl status crond
Add or update job in crontab (e=edit)
[root@asim ~]#crontab -e
Add or update job in crontab for another user
[root@asim ~]#crontab –u username -e
View crontab entries of current user (l=list)
[root@asim ~]#crontab -l
View crontab entries of another user
[root@asim ~]#crontab –u username –l
Remove crontable entries
[root@asim ~]#crontab -r
Add job to cron table
[root@asim ~]#crontab -e
Schedule the script to run at 1AM every Friday
minute:0hour:1, Day of month:*(every day of month),               
Month:*(every Month) Day:5(Friday)
[root@asim ~]#0 1 * * 5
Add job to cron table
[root@asim ~]#crontab -e
Runs every minute to create test.txt file
[root@asim ~]#* * * * * touch /test.txt
Runs at 6:30 am every day
[root@asim ~]#30 6 * * * touch /test1.txt
Runs at 6:30 pm every day
[root@asim ~]#30 18 * * *  touch /test.txt
Runs at 11 am everyday to remove all file from temp
[root@asim ~]#00 11 * * * rm –f /var/tmp/*

Leave a Reply

Your email address will not be published. Required fields are marked *