LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Returning a PID from PHP?? (https://www.linuxquestions.org/questions/programming-9/returning-a-pid-from-php-65328/)

Dirt 06-13-2003 12:34 AM

Returning a PID from PHP??
 
Hi I have a basic knowledge of PHP. Here is what I am trying to do.

I want to start and stop a program (game server) from my browser. I made a simple php script to start it.
<?
exec("./myprogram");
?>

This works great except I need a way to stop or kill this program from my web browser also. So I thought that If I could get it to return a PID when it runs it then I could simply KILL PID when I need to stop it..

Is this a good way to go about it or is there a simpler way?

Thanks DIRT

turnip 06-13-2003 04:48 AM

make a shell script with the following..

Code:


#!/bin/sh

ps -ef | grep myprogram |grep -v grep | awk '{print $1}' | xargs -n1 kill -9

then just

exec("./mykillscript");

that should help ya out.

remove the xargs and it willl just return the pid(s) of the process.

Dirt 06-13-2003 12:11 PM

Thanks,
I thought about grepping it out, but the problem is Im running 3 of the same programs (different IP's ) in the background. I also thought about grepping it by user and program name but since its started by apache the user is apache on all of them. The only difference was the PID and I thought if I could get the PID when they run the program and store it in a database, then later I could simply kill it. Is this possible?

Dirt

turnip 06-13-2003 01:22 PM

A few thoughts on this. Rename the program (script) for each instance so that you can grep it out? Or grep it by start time awk '{print $8}'. rewrite the script in c or c++ then you can ge tthe pid for each instance (if you want help with that i can put some time in). Or hmm well lemme think about it some more, im out of ideas at the moment heh.

turnip 06-13-2003 01:28 PM

Ok or put this in the startup script

Code:


printERROR() {
      echo "ERROR:" $@ >&2
}

getPID() {
      if [ $# -lt 1 ]; then
      printERROR "Insufficient Arguments."
      return 1
fi
/bin/ps -ax | grep "$1" |grep -v grep | awk '{print $1;}'
}

and then at the end of the script do a

getPID command > savefile and then use php to toss it into a database, using > rather than >> will overwrite the file with the new information so in theory it should always be the current pid of the last executed script. Lemme think some more on this. I am sure there is a good solution

Hko 06-14-2003 04:26 AM

Maybe it's an idea to make a script to start/stop the server using the "start-stop-deamon" utility? I think it's available in most distributions (on debian it's installed by default).

start-stop-daemon has options to create and read (for starting resp. stopping) the pid of the server process from a pid-file in /var/run.

To be able to select which server to stop, you could could have the script echo the IP-adres for which the server was started, so you can easily parse this in PHP when you run the script with popen().


All times are GMT -5. The time now is 04:15 AM.