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.

Était-ce utile?

La 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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top