Abdo
Abdo

Reputation: 14051

Store the forked process' id in file

I have the following but I'm worried about $! being overwritten prior to the echo command taking place (I know this is probably a few milliseconds). How do you guys recommend going about this?

sleep 100 & >/dev/null ; echo $! >sleep.pid

Upvotes: 1

Views: 741

Answers (1)

Jonathan Wakely
Jonathan Wakely

Reputation: 171263

$! is the PID of the most recent background process in the current shell, not the most recent on the entire box. That would be useless in practice, because you could never know if you got the right value.

The snippet you show in your question cannot possibly return the wrong PID. There is no other background process between the sleep and the echo.

Upvotes: 2

Related Questions