This guide will show you how to use the at command in Linux so that you can schedule jobs to run automatically at some point in the future.
Scheduling jobs is an essential part of administering Linux servers. We took a look at how to schedule jobs on Linux machine using the cron command earlier. Here’s an alternative to cron – at. The primary difference between the two is that when you schedule a task using cron it execute repeatedly without the need for rescheduling. With at, on the other hand, the scheduling of a task is only for a single execution. Both of these commands have their use, and I would suggest that you get a good understanding of them both.
Let’s look at how to schedule a task to execute only once using the at command. First make sure that the at daemon is running using a command like this:
# ps -ef | grep atd
root 8231 1 0 18:10 ? 00:00:00 /usr/sbin/atd
If you don’t see atd running start it with this command:
# /etc/init.d/atd start
Once the daemon has been started successfully you can schedule an at task using the two options -f, for the file to be executed, and -v, for the time at which it should be executed. So if you want to execute the shell script shellscript.sh at 6:30 PM you would run the following command:
# at -f shellscript.sh -v 18:30
Remember that with the at command the script shellscript.sh will execute at 6:30 PM and then the scheduling will disappear. So if this is not what you desire, you are better off using cron.
The at command is pretty clever in that it can take some orders in English if you like. For example, you can schedule jobs using the following syntax as well:
# at -f shellscript.sh 10pm tomorrow
# at -f shellscript.sh 2:50 tuesday
# at -f shellscript.sh 6:00 july 11
# at -f shellscript.sh 2:00 next week
You can see your job number using atq, then using atrm you can delete that job.
how can i cancel the schedule…?
I have another followup for you on use of the at command.
I’ve run into situations where I’ve needed a command run every hour BUT I needed the command to run ONLY if the previous run has completed. It might not be hung but it might be very very busy. I don’t mind skipping an hour’s execution if the process is still busy.
When this situation arises, I don’t use cron, I use ‘at’ and the last thing I put in my script is the at command itself to resubmit the job for execution.
Now, even if the job takes three hours, at the top of the following hour, the at command will launch the job as expected without cron jobs bumping into each other.
Yes, there’s ways to put the logic into a script for cron to deal with it but that can also be difficult to maintain and not as obvious (to some) as this approach.
If you don’t see atd running start it with this command:
# /etc/init.d/atd start
More correctly, if it isn’t running, it probably is not configured to startup when the system is restarted. Which may also keep the above command from working as expected. To correct this, use:
# chkconfig atd --level 234 on
# service atd start
and check again to make sure it is running, if it is, continue with the instructions (which are very good BTW).