how to Schedule task or job in Linux using at.


Schedule task using AT command..

“AT” allows Linux and Unix users to run commands or scripts at a given date and time. You can schedule scripts to be executed periodically. It is usually used for sysadmin jobs such as backups or cleaning /tmp/ directories and more.
“AT” is used to schedule the job for a particular time or interval, in other words it is used only for one time or only for one interval.

The disadvantages of at jobs are
  • It can be modified like cron jobs
  • It cannot be reused
  • The content cannot to viewed in normal human readable format


Schedule at job to display current date on current console “now”

To schedule above job using at first check the which console you are using…

#tty


[root@linuxelearn ~]# tty
/dev/pts/1
[root@linuxelearn ~]#


The Syntax to use at job for this task is

#at <time>
TASK
Ctrl+d to save it.

#at now


[root@linuxelearn ~]# at now
at> date > /dev/pts/1
at> <EOT>
job 4 at 2017-01-29 11:11
[root@linuxelearn ~]# Sun Jan 29 11:11:58 IST 2017


Schedule at job to get a mail at 04 : 30 PM regarding conference call.
It is very similar to above task, use the following command..

# at 04.30pm


[root@linuxelearn ~]# at 04.30pm
at> echo " conference call at 5 Pm"
at> <EOT>
job 5 at 2017-01-29 16:30
[root@linuxelearn ~]#


How to schedule at job to get a mail at 11.30 Am till three days from now for a metting.

#at 10.30am + 3days


[root@linuxelearn ~]# at 10.30am + 3days
at> echo "Meeting today 12 PM"
at> <EOT>
job 6 at 2017-02-01 10:30
[root@linuxelearn ~]#


How To check the list of at jobs

# at –l or # atq


[root@linuxelearn ~]# at -l
6       2017-02-01 10:30 a root
5       2017-01-29 16:30 a root
[root@linuxelearn ~]#


How to check what is scheduled in at job

# at –c <job id>
# at –c 6

Note : the output will not be in human readable format and also very lengthy


[root@linuxelearn ~]# at -c 6
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
HOSTNAME=linuxelearn.rj.com; export HOSTNAME
SELINUX_ROLE_REQUESTED=; export SELINUX_ROLE_REQUESTED
SHELL=/bin/bash; export SHELL
HISTSIZE=1000; export HISTSIZE
.
.
.
.
cd /root || {
         echo 'Execution directory inaccessible' >&2
         exit 1
}
${SHELL:-/bin/sh} << 'marcinDELIMITER417d7133'
echo "Meeting today 12 PM"

marcinDELIMITER417d7133
[root@linuxelearn ~]#


How to remove at jobs

First check the job it
 To remove a at job the syntax is:

#atrm <job id>
#atrm 6
#atrm 5


[root@linuxelearn ~]# atq
6       2017-02-01 10:30 a root
5       2017-01-29 16:30 a root
[root@linuxelearn ~]# atrm 6
[root@linuxelearn ~]# atrm 5
[root@linuxelearn ~]# atq
[root@linuxelearn ~]#


How to restrict a user for using at jobs.

For restricting a user for using at job use following methods.
  • To restrict a user from using at jobs it is exactly same like cron jobs
  • Add user names to /etc/at.deny , It will work like exactly same like cron.deny.
  • To allow only few out of many users remove at.deny like we do for cron.deny and create /etc/at.allow and add user names who are allowed to use at jobs in it, like cron.allow.
  • If both at.allow and at.deny exists, then at.allow will have higher priority.
  • If neither at.allow nor at.deny exists, then only root can use at jobs.


All the above are few examples of at jobs, keep practicing to know more about it… If you have any problem then comment me..

  

Post a Comment