Unlike cron, which let us run a task on a regular basis, at gives us the ability to execute a command or a script at a specified date and hour, or after a given interval of time. Minutes, hours, days or weeks can be used as units. It’s even possible to use certain “keywords” as midnight or teatime.
At command 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 command with useful examples.
Install at command
If not installed by default, at should be available in almost all distributions’ repositories.
Install at on Fedora
To install it on Fedora, just run:
# dnf install at
Install at on CentOS or RHEL linux
On RHEL or CentOS yum is still the default package manager:
# yum install at
Install at on Ubuntu, Debian and Linux Mint
To install it on Debian, Ubuntu and Linux Mint run command:
$ sudo apt-get install at
Starting at daemon
Once the program it’s installed, we must start the atd daemon and eventually enable it if we want it to be launched automatically at boot.
--------- On SystemD --------- # systemctl start atd # systemctl enable atd
--------- On SysVinit --------- # service atd start # chkconfig --level 35 atd on
[ads]
Commands used with at
at – execute commands at specified time.
atq – lists the pending jobs of users.
atrm – delete jobs by their job number.
Schedule first job using at command
Below example will schedule “sh backup.sh” command to be executed on next 10:00 AM once.
$ at 10:00 AM warning: commands will be executed using /bin/sh at> sh backup.sh at> job 2 at Tue Jan 15 10:00:00 2019
Use ^d to exit from at prompt.
You can also use following option to schedule job. The below command will run “sh backup.sh” at 10:00 in the morning.
Manage scheduled jobs
To queue, examine or delete jobs scheduled with at, we can either use dedicated commands like atrm and atq or run at with specific flags, the latter being just aliases for the former. For example, say we want to obtain a list of all pending jobs scheduled with at by our user:
$ atq 2 Tue Jan 15 10:00:00 2019 a rasho
The above command, if launched as root, will display the task scheduled by all users in the system.
To delete a queued job, we could use atrm or run at with the equivalent flags: -r or -d. The job to be deleted must be referenced by its number. In the case above, we would therefore run:
$ atrm 2
Check the content of scheduled at job
atq command 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.
$ at 10:00 AM
Example 2: Schedule task at 10:00 AM on coming Sunday.
$ at 10:00 AM Sun
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 1/14/2019 $ at 10:00 AM 1.14.2019
[ads]
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
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.
$ 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’ command in Linux.
What are the differences between at-command and chrontab?