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.

When I detach command:

$ command &

The result is a printout to STDOUT:

[1] < PID >

I would like to monitor this PID. However, I cannot write this output anywhere but STDOUT! How can I redirect this to a file or a variable?

I have tried:

$ command 1> ./temp.txt &

But this still finds its way to STDOUT!

Thanks in advance for the help!

share|improve this question
add comment

1 Answer

up vote 9 down vote accepted

$! should contain the PID of the last background process. Something like...

$ command &
$ echo $! > temp.txt

Should give you a file named temp.txt with the PID in it.

Or on one line...

$ command & echo $! > temp.txt
share|improve this answer
    
Awesome. This does not answer my exact question, but this is even better! –  Daeden Jul 17 '13 at 6:44
    
Yeah, I think the problem is stdout is the output of the command you're running, but the id is coming from the shell. So to capture it with stdout you'd have to actually execute it in a subshell some how and capture the stdout of that. Just thinking out loud about what's going on, haven't tested that. –  Bill Heller Jul 17 '13 at 14:29
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.