Question

OK, so here's what I want...

I'm triggering the background execution of a php script like this :

/usr/bin/php myscript.php > /dev/null &

The script runs for some time, and stops when finished.

However, I'd really need to have its process id (PID) so that I could kill the process (if needed) by :

kill -SIGTERM PID

However, how do I get the PID of that particular process?


NOTE: There may be numerous php processes at any time, so targetting all instances running is not what we need.

Was it helpful?

Solution

Save the PID of your background process at the time you start it.

  • $$ is the current script's pid
  • $! is the pid of the last background process

For instance :

/usr/bin/php myscript.php > /dev/null &
LAST_PID=$!
kill $LAST_PID
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top