Scheduling Tasks with at is one way we can defer user jobs. In Red Hat Enterprise Linux 8 we can defer jobs to run at later times using cron, systemd or at, in this blog and video we look at scheduling tasks with at.
Not all scheduled jobs in Linux need to run regularly. Regularly occurring jobs can be run using systemd timers or crond. For deferred user jobs that often need to be run at a single time we have the atd service and the at command. For any job to run via the atd service, the service must be running. We can check the status of the service with the status sub-command from systemctl but don’t forget we can also check the service is enabled and active independently:
# systemctl is-active atd active # systemctl is-enabled atd enabled
When creating scheduled tasks with the at command we need to define when it will run, we can specify the complete date or time just some parts of it, for example, to run the command date at 13:57 tomorrow we simply use:
# at 13:57 tomorrow warning: commands will be executed using /bin/sh at> date > /home/tux/date-file at> <EOT> job 1 at Fri Nov 29 13:57:00 2019
To exit the interactive command we use CTRL+D. To run a job at 05:00 on the next 27th December we can use the following format, noting that we can run multiple commands at any data and time specified:
# at 05:00 27 Dec warning: commands will be executed using /bin/sh at> ls /etc at> du -sh /etc at> <EOT> job 2 at Fri Dec 27 05:00:00 2019
To list jobs we can use atq and atrm to remove jobs:
# atq 1 Fri Nov 29 13:57:00 2019 a root 2 Fri Dec 27 05:00:00 2019 a root # atrm 1 2 # atq
There are many formats that we can use to specify when jobs should run, the following show a few examples:
$ at now +1 hour warning: commands will be executed using /bin/sh at> tar -czf $HOME/scripts.tgz $HOME/bin at> <EOT> job 2 at Wed Dec 11 20:14:00 2019 $ at teatime tomorrow warning: commands will be executed using /bin/sh at> find $HOME/ -type f -name "*.txt" -delete at> <EOT> job 3 at Thu Dec 12 16:00:00 2019 $ at 9 am 25 December 2020 warning: commands will be executed using /bin/sh at> wall "Happy Christmas" at> <EOT> job 4 at Fri Dec 25 09:00:00 2020
The time reference teatime is 4pm, how quaint! The now +1 hour style is quite useful, if the backup is going to take a while we can schedule it to happen as we leave work. We know that the backup will be ready for us the next day and we have not had to spend a lot of time thinking of formatting the date and time.
To list the contents of an at job we can use the option at -c and the job number.