Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer 1

up vote 2 down vote accepted

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

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

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 test.sh 2>&1 | awk '/job/ {print $2}'`
$ echo $TEST
9
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 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 at 20:32
    
Thanks for the invitation. I'll do that now. –  nwk Feb 27 at 20:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.