r/smallprog • u/sedmonster • Mar 16 '10
[bash] Remember the pid of a process.
Use $! to get the process id of the last process run in the background.
For instance, remember the pid:
sleep 10000 &
echo $! > pid.txt
And kill the process later:
kill -9 `cat pid.txt`
Edit: Added clarification about background.
5
Upvotes
View all comments
Show parent comment
5
u/sedmonster Mar 16 '10
Because you may have run other background processes since the one in question. Because the value of $! is then reset, you save the original pid for later termination.