Written by James McDonald

February 22, 2011

I am looking at the Linux at command again.

I used to think that you couldn’t create commands with arguments and feed them to at without using at -f filewithcommandsinit but I have discovered it’s quite easy using STDIN e.g.


# to add a new command to run 1 minute from now use echo and then the command
# by escaping the redirection you can include that also
 jm@bb:~$  echo echo this is a test \> /tmp/test | at -q z now + 1 minute
# here is an example of something that might do real work
# echo sudo /etc/rc.d/init.d/pptpd start | at -q p now + 4 hours
# you would have to add the user to your sudoers file with:
# username ALL=NOPASSWD:/etc/rc.d/init.d/pptpd 
# otherwise `at' would fail and try to launch a password prompt
warning: commands will be executed using /bin/sh
job 4 at Tue Feb 22 19:31:00 2011

# to list your at jobs use atq
jm@bb:~$ atq
4	Tue Feb 22 19:31:00 2011 z jm

# to view the contents of you job use at -c
jm@bb:~$ at -c 4
#!/bin/sh
# atrun uid=1000 gid=1000
# mail jm 0
umask 22
# a heck of a lot of environment trimmed but finally you get the command you passed in to at
...
echo this is a test > /tmp/test

Deleting all prior at commands from a certain queue
This one-liner lists the jobs cuts the job numbers out and feeds them to xargs which in turn give them to atrm to remove the jobs.

atq -q p | cut -f1 | xargs -IJOB atrm JOB
# e.g. 

# given a list of at jobs as shown
jm@bb:~$ atq
17	Tue Feb 22 19:41:00 2011 z jm
13	Tue Feb 22 19:41:00 2011 z jm
14	Tue Feb 22 19:41:00 2011 z jm
15	Tue Feb 22 19:41:00 2011 z jm
18	Tue Feb 22 19:41:00 2011 z jm
16	Tue Feb 22 19:41:00 2011 z jm

# run the one-liner on them
jm@bb:~$ atq -q p | cut -f1 | xargs -IJOB atrm JOB
jm@bb:~$ atq # they should be gone but their not because I specified the wrong queue with -q p
# instead of -q z 
17	Tue Feb 22 19:41:00 2011 z jm
13	Tue Feb 22 19:41:00 2011 z jm
14	Tue Feb 22 19:41:00 2011 z jm
15	Tue Feb 22 19:41:00 2011 z jm
18	Tue Feb 22 19:41:00 2011 z jm
16	Tue Feb 22 19:41:00 2011 z jm
# get it right
jm@bb:~$ atq -q z | cut -f1 | xargs -IJOB atrm JOB
jm@bb:~$ atq
# and they are all deleted

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Squarespace Image Export

To gain continued access to your Squarespace website images after cancelling your subscription you have several...

MySQL 8.x GRANT ALL STATEMENT

-- CREATE CREATE USER 'tgnrestoreuser'@'localhost' IDENTIFIED BY 'AppleSauceLoveBird2024'; GRANT ALL PRIVILEGES ON...

Exetel Opt-Out of CGNAT

If your port forwards and inbound and/or outbound site-to-site VPN's have failed when switching to Exetel due to their...