LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2003, 12:34 AM   #1
Dirt
Member
 
Registered: Feb 2002
Location: St. Louis
Distribution: Red Hat 8.0
Posts: 30

Rep: Reputation: 15
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
 
Old 06-13-2003, 04:48 AM   #2
turnip
Member
 
Registered: Jul 2002
Posts: 143

Rep: Reputation: 15
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.

Last edited by turnip; 06-13-2003 at 04:49 AM.
 
Old 06-13-2003, 12:11 PM   #3
Dirt
Member
 
Registered: Feb 2002
Location: St. Louis
Distribution: Red Hat 8.0
Posts: 30

Original Poster
Rep: Reputation: 15
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
 
Old 06-13-2003, 01:22 PM   #4
turnip
Member
 
Registered: Jul 2002
Posts: 143

Rep: Reputation: 15
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.
 
Old 06-13-2003, 01:28 PM   #5
turnip
Member
 
Registered: Jul 2002
Posts: 143

Rep: Reputation: 15
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
 
Old 06-14-2003, 04:26 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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().

Last edited by Hko; 06-14-2003 at 04:30 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Returning very strange Values in PHP Script benrose111488 Programming 2 10-21-2004 11:26 AM
/var/run/[XXX].pid - Tcl pid code liguorir Linux - Software 1 05-20-2004 10:32 PM
rm cannot remove /var/run/atd.pid and /var/run/xdm.pid danishmr Linux - Software 1 05-04-2004 08:01 AM
ERROR: Couldn't write pid to pid file lawrencegoodman Linux - Newbie 2 02-13-2004 08:05 PM
having trouble returning... jhorvath Programming 12 10-26-2002 10:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration