Scheduling jobs is an integral part of any server administrator’s day. There are dozens of things that need to be done on a regular basis – backups, cleanups, log file renamings, virus scans etc etc. For all these regularly occurring tasks, Linux has the well known “cron” utility. We’ve already gone over this in detail in one of my earlier tutorials regarding cron. If you need something done at a specific time at specific intervals, cron is the tool for you.
However, what if you need to do something just once at a future date? Sure, you can still set up a cron job for it, but it seems like overkill does it not? You’ll have a permanent entry sitting in your cron configuration file for a job that only needs to be run once! You have to remember to remove it once it’s done. All in all, a very messy solution.
For situations like this, we have another command – at.
Using “at” To schedule One-Time Jobs
First, check if the “at” tool is installed on your system. If you get a command not found, you can most likely use your package manager to install it. For example, CentOS just type:
yum install at
Once you have the “at” tool installed on your system, we need to start and enable the service like this:
systemctl start atd systemctl enable atd
Now you can schedule any set of commands or scripts in the future by typing:
Where [time] refers to the time when you want the command to run. The format is quite generous – you just need to remember that we first specify the hour/minutes and then the day/date. For example, all the above examples will work:
at tomorrow at next thursday at 5:00 PM 10/16/2017 at now + 2 days
Truth be told, the entire formal specification for the time format is pretty complicated. You can find the official format in the following file on your server:
The part in bold refers to the version of at – it might be different for you. I suggest you navigate to the /usr/share/doc folder and find out what it is exactly. But if you don’t think too hard about it, the format is kind of “obvious”. Just use the above examples as a starting point and you should be fine!
Once you type the above command, you’ll get a special command prompt like this:
Here, you can type in whatever commands you want that will be executed at the mentioned time. Pressing “Enter”, will allow you to type in additional commands – one on each line. You can also execute scripts! When you’re done, exit by pressing “Ctrl+d” and the job will be scheduled as shown below:
Viewing Existing Scheduled Jobs
If at any time you want to see how many jobs are currently scheduled using “at”, you can use the following command:
This will bring up a list of scheduled tasks along with the time of execution, and under which user they’ll be running:
However, this doesn’t show you the command details. To get that, we use the following:
at -c [job number]
Where [job number] refers to the number on the left of each job in atq. So in my example, “at -c 2” will show the following:
It actually displays the complete output of what is going to be executed, so there’s a lot of additional stuff shown. But the final commands to be executed are displayed at the very end as you can see.
Finally, you can remove any job from the current “atq” display by using:
at -r [job number]
Where [job number] refers to the number in the “atq” command.
As you can see, compared to cron, “at” is great at creating quick scheduled jobs without much hassle!
Is your website slow?
Enter its URL below to find out now:
- PREVIOUS ENTRY: How to Add a Directory to PATH Variables in Linux
- NEXT ENTRY: How to Use a WordPress Login Form Captcha with Google