Você está na página 1de 7

1/2/2018 One Time Task Scheduling using at Command in Linux – TecAdmin

One Time Task


Scheduling using at
Command in Linux

While working on Linux systems we preferred crontab for


scheduling jobs generally. There are another utility at
c o m m a n d is very useful for scheduling one time tasks. It
reads co mm an d s from standard input or script/file
which can be executed later once. But we can’t use at
c o m m a n d for any recurring tasks. For recurring tasks use
Linux crontab.

Read This => Examples to schedule recurring tasks


with crontab

1/11
1/2/2018 One Time Task Scheduling using at Command in Linux –

At c o m m a n d can be useful for shutdown system at


specified time, Taking one time backup, sending email as
reminder at specified time etc. This article will help you to
understand the working of at c o m m a n d with useful
examples.

Commands used with at:


at : execute co m m and s at specified time.
atq : lists the pending jobs of users.
atrm : delete jobs by their job number.

1. Schedule first job using at command


Below example will schedule “sh backup.sh” c o m m a n d to
be executed on next 9:00 AM once.

# at 9:00 AM
at> sh backup.sh
at> ^d
job 3 at 2013-03-23 09:00

Use ^d to exit from at prompt.


You can also use following option to schedule job. The
below c o m m a n d will run “sh backup.sh” at 9:00 in the
morning.

# echo "sh backup.sh" | at 9:00 AM

2. List the scheduled jobs using atq


When we list jobs by root account using atq , it shows
all users jobs in result. But if we execute it from non root
account, it will show only that users jobs.

https://tecadmin.net/one-time-task-scheduling-using-at-commad-in-linux/ 2/11
1/2/2018 One Time Task Scheduling using at Command in Linux – TecAdmin

# atq

3 2013-03-23 09:00 a root


5 2013-03-23 10:00 a rahul
1 2013-03-23 12:00 a root

Fields description:
First filed: job id
Hosted@DigitalOcean

Second filed: Job execution date


third filed: Job execution time
Last field: User name, under which job is scheduled.

3. Remove scheduled job using atrm


You can remove any at job using atrm with their job id.

# atrm 3
# atq

5
2013-03-23 10:00 a rahul
1
2013-03-23 12:00 a root

4. Check the content of scheduled at job /

a t q c o m m a n d only shows the list of jobs but if you want


to check what script/commands are scheduled with that
task, below example will help you.

# at -c 5

In above example 5 is the job id.

Examples of at Command:
Example 1: Schedule task at coming 10:00 AM.
3/11
1/2/2018 One Time Task Scheduling using at Command in Linux – TecAdmin

# at 10:00 AM

Example 2: Schedule task at 10:00 AM on coming


Sunday.

# at 10:00 AM Sun
Hosted@DigitalOcean

Example 3: Schedule task at 10:00 AM on coming


25’th July.

# at 10:00 AM July 25

Example 4: Schedule task at 10:00 AM on coming


22’nd June 2015.

# at 10:00 AM 6/22/2015
# at 10:00 AM 6.22.2015

Example 5: Schedule task at 10:00 AM on same date at


next month.

# at 10:00 AM next month

Example 6: Schedule task at 10:00 AM tomorrow.

# at 10:00 AM tomorrow

Example 7: Schedule task at 10:00 AM tomorrow.

# at 10:00 AM tomorrow

Example 8: Schedule task to execute just after 1 hour.

# at now + 1 hour

https://tecadmin.net/one-time-task-scheduling-using-at-commad-in-linux/ 4/11
1/2/2018 One Time Task Scheduling using at Command in Linux – TecAdmin

Example 9: Schedule task to execute just after 30


minutes.

# at now + 30 minutes

Example 10: Schedule task to execute just after 1 and


2 weeks.
Hosted@DigitalOcean

# at now + 1 week
# at now + 2 weeks

Example 11: Schedule task to execute just after 1 and


2 years.

# at now + 1 year
# at now + 2 years

Example 12: Schedule task to execute at mid night.

# at midnight

Above job will execute on next 12:00 AM

Thanks for reading this article, I hope you will understand


to how to use ‘at’ c o m m a n d in Linux.

5/11
1/2/2018 One Time Task Scheduling using at Command in Linux – TecAdmin

I just wanted to extend it a bit with some useful additions. 1) You can use - f
option to point “at” to the script you need
to run:
at - f /path/to/the/script time_spec
2) One can use “at” to start a process in background without nohup, etc.
As easy as
at - f /a /c om m and now
or
echo “/a/command” | at now
3) You can use “at” to run a c om m a n d repeatedly, but unlike cron you can use “
at” to run c om m ands with some period between runs, for example after 3
minutes after previous run was completed. This allow you to avoid various
checks preventing next run to start before previous is finished.
Moreover you can define this period as random value. Examples:
The script (lets nam e it /home/user1/at_run.sh):
————————————-
#!/bin/bash
/the/command/you/need
# fixed period between runs
period=3
# or random period. RANDOM is a bash’s random number from 0 to 32767
period=$[ ($RANDOM % 20) + 15 ]
at - f /home/user1/at_run.sh now + $period minutes
————————————-
run /home/user1/at_run.sh and all next runs will be scheduled
automatically, so your
/the/command/you/need will run repeatedly forever. Sure, you can break the
next run with atq/atrm.

6/11

Você também pode gostar