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.

有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top