Scheduling Jobs Using at Command in Linux

Channel: Linux
Abstract: job 3 at 2013-03-23 09delete jobs by their job number. 1. Schedule a Job Using ‘at’ Command The below example will schedule the backup.sh command to b

While working with Linux systems we preferred crontab for scheduling jobs generally. There are another utility at command is very useful for scheduling one time tasks. It reads commands from standard input or script/file which can be executed later once. But we can’t use at command for any recurring tasks. For recurring tasks use Linux crontab.

  • Read This => Schedule recurring tasks with crontab on Linux

At command can be useful for shutdown system at the specified time, Taking a one-time backup, sending an email as a reminder at the specified time, etc. This article will help you to understand the working of at command with useful examples.

Commands used with at:
  • at : execute commands at specified time.
  • atq : lists the pending jobs of users.
  • atrm : delete jobs by their job number.
1. Schedule a Job Using ‘at’ Command

The below example will schedule the backup.sh command to be executed at 9:00AM once.

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

Use ^d to exit from at prompt.
You can also use the following option to schedule a job. The below command will run the backup.sh script 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 user’s jobs in the result. But if we execute it from a non-root account, it will show only that user’s jobs.

atq
Output
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
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 job using atrm with their job id.

atrm 3
atq
Output
5       2013-03-23 10:00 a rahul
1       2013-03-23 12:00 a root
4. Check the content of scheduled at job

The atq command only shows the list of jobs but if you want to check what script/commands are scheduled with that task, the below example will help you.

at -c 5 

In the above example, 5 is the job id.

Examples of at Command:
  1. Schedule task at coming 10:00 AM.
    at 10:00 AM 
    
  2. Schedule task at 10:00 AM on coming Sunday.
    at 10:00 AM Sun 
    
  3. Schedule task at 10:00 AM on coming 25’th July.
    at 10:00 AM July 25 
    
  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 
    
  5. Schedule task at 10:00 AM on the same date as next month.
    at 10:00 AM next month 
    
  6. Schedule task at 10:00 AM tomorrow.
    at 10:00 AM tomorrow 
    
  7. Schedule task at 10:00 AM tomorrow.
    at 10:00 AM tomorrow 
    
  8. Schedule task to execute just after 1 hour.
    at now + 1 hour 
    
  9. Schedule task to execute just after 30 minutes.
    at now + 30 minutes 
    
  10. Schedule tasks to execute just after 1 and 2 weeks.
    at now + 1 week 
    at now + 2 weeks 
    
  11. Schedule tasks to execute just after 1 and 2 years.
    at now + 1 year 
    <at now + 2 years 
    
  12. Schedule tasks to execute at midnight.
    at midnight 
    

    The above job will execute at 12:00 AM

Thanks for reading this article, I hope you will understand how to use the ‘at’ command in Linux.

Ref From: tecadmin

Related articles