Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to have the at command return somehow the job id it just submitted when used from within a script? (kind of like $? retrieves the last exit code or $$/$! retrieve the PID of the command just executed).

share|improve this question
When I run echo "touch foo" | at now, I get e.g. job 3 at Thu Dec 15 17:14:52 2011 as output — you don't? Likewise inside a script, it writes to standard output, from where it can be parsed. – Daniel Beck Dec 15 '11 at 16:16
up vote 2 down vote accepted

Assuming that the job you want to run is in a file called, the following will return the id:

 $ at now -f 2>&1 | awk '/job/ {print $2}'

The 2>&1 redirects stderr to stdout so you can manipulate it. The awk returns the second field in the line that has the word "job" in it, which is the line that contains the job id.

So it get it into a variable, you can do:

$ TEST=`at now -f 2>&1 | awk '/job/ {print $2}'`
$ echo $TEST
share|improve this answer
No need to pipe at's output through grep: awk '/job/ {print $2}' will, erm, do the job. – nwk Feb 27 '14 at 20:26
@nwk Hi nwk, welcome to Superuser. If you see an opportunity to improve an answer, feel free to go ahead and edit. This definitely counts :-) – Paul Feb 27 '14 at 20:32
Thanks for the invitation. I'll do that now. – nwk Feb 27 '14 at 20:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .