Saving a process ID when detaching a command in Bash

bashcommand linecygwin;

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!

Best Answer

$! 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
Related Question