Web Hosting Talk







View Full Version : Stopping a Linux Process using PHP


MarkioE
01-20-2007, 04:46 PM
Hello. I am running a game server and am trying to create a basic control panel which will allow me to:

Start
Restart
Stop

the server. I am not really sure how to do this, and was wondering if people could just tell me the basics of what I need to do :)

Thanks guys.

scribby
01-21-2007, 11:37 AM
I guess you could use exec() ?

MarkioE
01-21-2007, 12:46 PM
Thanks, thats what I was looking for. BUT if the game server is running as a process, how do I kill/close that process using php?

scribby
01-21-2007, 01:01 PM
Well you would need to record the process ID in a database when its started somehow then when it comes around to stopping the process just use the command you normally would to stop a process in SSH.

MarkioE
01-21-2007, 02:54 PM
Well you would need to record the process ID in a database
Do you know any methods of doing this?

hehachris
01-22-2007, 02:20 PM
don;t expect others to do the work for you. What we should to is to point you to the right direction.

Try
ps aux | awk '$11=="your_command"' | awk '{print $2}'

and you will get a list of the process numbers that the game generated (your_command).

you can either save the output into a temp file(and then use file() to loop into it) or parse the output directly in your PHP. At last store them into a table.

Make sure your PHP should have root access (may be sudo or write a setuid wrapper).

arkin
01-23-2007, 03:00 AM
When launching a server you normally pass a ip and a port if I remember rightly, I'm no grep wizard but something like this may work:


exec('ps -aux | grep "127\.0\.0\.1.*27015"');


Now, gather the return from that aka. the PID.


exec('kill -SEGV PID');


And that will stop the server.

MarkioE
01-23-2007, 11:10 AM
don;t expect others to do the work for you. What we should to is to point you to the right direction.

Try
ps aux | awk '$11=="your_command"' | awk '{print $2}'

and you will get a list of the process numbers that the game generated (your_command).

you can either save the output into a temp file(and then use file() to loop into it) or parse the output directly in your PHP. At last store them into a table.

Make sure your PHP should have root access (may be sudo or write a setuid wrapper).

I wasn't expecting anyone to do the work, I was just asking what I needed to look up.

Thanks alot for your help, this is the method I will be using.

MarkioE
01-23-2007, 12:50 PM
Ok, I have another question.

How can I kill all of these processes:

2267
2275
10056
10058

using one command?

Thanks.



EDIT:
This is what I have so far

<?php

$output = shell_exec("ps aux | awk '/27015/' | awk '{print $2}'");
echo "<pre>$output</pre>";

shell_exec("kill -SEGV $output");

$output2 = shell_exec("ps aux | awk '/27015/' | awk '{print $2}'");
echo "<pre>$output2</pre>";
?>

Burhan
01-24-2007, 01:29 AM
$foo = explode("\n",$output);
foreach($foo as $pid) { shell_exec("kill -SEGV ".trim($pid)); }

MarkioE
01-24-2007, 07:45 AM
$foo = explode("\n",$output);
foreach($foo as $pid) { shell_exec("kill -SEGV ".trim($pid)); }


I tried that, but it didn't actually kill the process.

scribby
01-24-2007, 12:17 PM
I tried that, but it didn't actually kill the process.

PHP would not have permission to use that command..
If it did imagine what a hosting client could do with just php and exec ;)

I'm not sure how you would set the permissions either, sorry.