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!

1 Answer 1


$! 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
  • Awesome. This does not answer my exact question, but this is even better!
    – Daeden
    Jul 17, 2013 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. Jul 17, 2013 at 14:29
  • Appears to me the redirect maze over here is doing a more complicated version of this. Thank you!
    – cautionbug
    Feb 9, 2021 at 15:05

You must log in to answer this question.

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