Joel Hackney
Joel Hackney

Reputation: 147

How to restart background php process? (how to get pid)

I'm a PHP developer, and know very little about shell scripting... So I appreciate any help here.

I have four php scripts that I need running in the background on my server. I can launch them just fine - they work just fine - and I can kill them by looking up their PID.

The problem is I need my script to, from time to time, kill the processes and restart them, as they maintain long standing HTTP requests that sometimes are ended by the other side.

But I don't know how to write a command that'll find these processes and kill them without looking up the PID manually.

We'll start with one launch command :

/usr/local/php5/bin/php -f /home/path/to/php_script.php > /dev/null &

Is there a way to "assign" a PID so it's always the same? or give the process a name? and how would I go about writing that new command?

Thank you!

Upvotes: 0

Views: 3106

Answers (5)

user1972251
user1972251

Reputation: 1

What I do is have my script check for a file that I name "run.txt". If it does not exist, they exit. Then just br renaming that (empty) file, I can stop all my scripts.

Upvotes: 0

Joel Hackney
Joel Hackney

Reputation: 147

OK! so this has been a headache and a half for my who knows NOTHING about shell/bash whatever scripting...

@redShadow 's response would had been perfect, except my hosting provider will not give me access to the /etc/supervisor/ directory. as he said, you must be root - and even using sudo was an admin wouldn't let me make any chances there...

Here's what I came up with:

kill -9 `ps -ef | grep php | grep -v grep | awk '{print $2}'`

because the only types of commands I was executing showed up in the top command as php this command loops thru running processes, finds the php commands and their corresponding PIDs and KILLS them! woot!!

Upvotes: 0

redShadow
redShadow

Reputation: 6787

Nope, you can't "assign" the process PID; instead, you should do as "real" daemons do: make your script save its own PID in some file, and then read it from that file when you need to kill.

Alternative would be to use something like supervisor, that handles all that for you in a quite nice way.

Update - supervisor configuration

Since I mentioned supervisor, I'm also posting here a short supervisor configuration file that should do the job.

[program:yourscriptname]
command=/usr/local/php5/bin/php -f /home/path/to/php_script.php

Have a look here for more configuration options.

Then you can use it like this:

# supervisorctl status

to show the process(es) status.

# supervisorctl start yourscriptname

to start your script

# supervisorctl stop yourscriptname

to stop your script

Update - real world supervisor configuration example

First of all, make sure you have this in your /etc/supervisor/supervisord.conf.

[include]
files = /etc/supervisor/conf.d/*.conf

if not, just add those two lines and

mkdir /etc/supervisor/conf.d/

Then, create a configurtion file for each process you want to launch:

/etc/supervisor/conf.d/script1.conf

[program:script1]
command=/usr/local/php5/bin/php -f /home/path/to/php_script.php
stdout_logfile=/var/log/script1.log
stderr_logfile=/var/log/script1-error.log

/etc/supervisor/conf.d/script2.conf

[program:script2]
command=/usr/local/php5/bin/php -f /home/path/to/php_script2.php
stdout_logfile=/var/log/script2.log
stderr_logfile=/var/log/script2-error.log

...etc, etc.. for all your scripts.

(note that you don't need the trailing & as supervisor will handle all the daemonization thing for you; in fact you shouldn't execute programs that are self-daemonizing inside supervisor).

Then you can start 'em all with:

supervisorctl start all

or just one with something like:

supervisorctl start script1

Starting supervisor from php

Of course, you can start/stop the supervisor-controlled processes using the two commands above, even from inside a script.

Remember however that you'll need root privileges, and it's quite risky to allow eg. a web page to execute commands as root on the server..

If that's the case, I recommend you have a look at the instructions on how to run supervisor as a normal user (I never did that, but you should be able to run it as the www-data user too..).

Upvotes: 4

Landon
Landon

Reputation: 4108

A work around to this would be to use ps aux, this will show all of the processes with the command that called them. This presumes of course that the 4 scripts are different files, or can be uniquely identified by the command that called them. Pipe that through a grep and you're all set ps aux | grep runningscript.php

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799570

The canonical way to solve this is to have the process write its PID into a file in a known location, and then any utility scripts can look up the file, read the PID, and manipulate that process. Add a command line argument to the script that gives the name of the PID file to write to.

Upvotes: 3

Related Questions