Most Linux admins are well-versed with
crontab. All too often, however, I see people using crontab to schedule a job that's only intended to run once. This not only clutters your cron file but can potentially be dangerous if you forget to un-schedule your job before it runs again.
Luckily there's an alternative:
at
Prerequisites:o Familiarity with the bash shell |
The
at command simply runs a job once at some specified point in the future. Unlike crontabs, which will only use your user environment if you explicitly tell it to,
at will use your environment by default.
The basic invocation patterns of
at are:
Listing 1: Basic invocation:1) at time
2) at time < /file/with/commands
3) echo "some command" |at time |
In the first example after pressing enter you will be dropped into the
at> shell. Here type the commands, one per line, you would like to be executed.
The second example is non-interactive. Instead of typing your commands into the
at> shell, put them into the /file/with/commands file instead.
Note that you do not put a shabang (#!/bin/sh) at the top of your script! If you have a script you want to run, do not specify "at
time < /path/to/script"--instead create a new text file with one line, "/path/to/script" in it, and use that file as your source to at (at
time < /path/to/newfile).
The third example is useful if you want to run a single command at a specified time. For example, "echo /path/to/script |at
time"
There are a number of ways you can specify the time an at job runs at. Below are the most common specifications (all these examples use the first formatting of listing 1 and will drop you into the
at> shell):
Listing 2: Time specifications:at 2pm (execute the job at 2pm today, or tomorrow if 2pm has already passed)
at +5 hours (execute the job exactly 5 hours from now)
at monday (execute the job on the coming Monday, next Monday if it is currently Monday)
at next month (execute the job exactly 1 month from now)
at 7/7/17 (execute the job on July 7th, 2017)
at 2:21pm september 21 (execute the job at exactly 2:21pm on September 21st)
at +1000 days (execute the job exactly 1000 days from now) |
To see a listing of scheduled at jobs, use the
atq command.
Listing 3: The atq commanddranok@neptune:~> atq
17 2017-07-07 12:38 a dranok |
To view the contents of an at job, use the
at -c jobid command. Your full environment will be included in this output, however I have snipped this from the listing below to remove the clutter.
Listing 4: View the contents of an at jobdranok@neptune:~> at -c 17
#!/bin/sh
# atrun uid=1000 gid=100
# mail dranok 0
umask 22
ls -l |
To remove an at job, use the
atrm jobid command.
Listing 5: Delete an at jobdranok@neptune:~> atrm 17
dranok@neptune:~>atq
dranok@neptune:~> |
At commands will persist through reboots. They are stored in a spool directory on the filesystem, generally /var/spool/atjobs. You can cat these jobs directly instead of using
at -c jobid. You can also remove them with rm instead of
atrm jobid.
Listing 5: Delete an at jobdranok@neptune:~> sudo ls -l /var/spool/atjobs
total 8
-rwx------ 1 dranok users 4966 Jul 24 12:43 a00012012d7160
dranok@neptune:~> sudo cat /var/spool/atjobs/a00012012d7160
#!/bin/sh
# atrun uid=1000 gid=100
# mail dranok 0
umask 22
Environment snipped to save space
ls -l
dranok@neptune:~> atq
18 2007-07-24 17:00 a dranok
dranok@neptune:~> sudo rm /var/spool/atjobs/a00012012d7160
dranok@neptune:~> atq |
Thanks for reading, and welcome to the wonderful world of at ;)
See Also: Relevant section of the at man page regarding time specifications At allows fairly complex time specifications, extending the POSIX.2 stan‐
dard. It accepts times of the form HH:MM to run a job at a specific time
of day. (If that time is already past, the next day is assumed.) You may
also specify midnight, noon, or teatime (4pm) and you can have a time-of-
day suffixed with AM or PM for running in the morning or the evening. You
can also say what day the job will be run, by giving a date in the form
month-name day with an optional year, or giving a date of the form MMDDYY
or MM/DD/YY or DD.MM.YY. The specification of a date must follow the
specification of the time of day. You can also give times like now +
count time-units, where the time-units can be minutes, hours, days, or
weeks and you can tell at to run the job today by suffixing the time with
today and to run the job tomorrow by suffixing the time with tomorrow.
For example, to run a job at 4pm three days from now, you would do at 4pm
+ 3 days, to run a job at 10:00am on July 31, you would do at 10am Jul 31
and to run a job at 1am tomorrow, you would do at 1am tomorrow. |
FUCKING IDIOT.
No I really did quit and sold my shit for IRL DOLLARS!