views:

47

answers:

3

What I need to have happen: PHP needs to launch an server app which has root permissions running in the background. All of this should be silent.

-Sudo is needed to allow php to perform an op that requires root permissions.

-Screen is required to allow the app to run outside the scope of the webpage which started the process.

-Expect is needed so that screen has a pts in which to run

-Sh is needed because whatever starts running needs to be backgrounded, presumably with the & operator. It would also need to pipe any output to /dev/null/since I don't want my PHP page returning anything. This is probably negotiable somehow if somebody can think of a better way to do the call in PHP (fork...?)

As an example, the script I tried to use was:

#!/usr/bin/expect -f
spawn sh ( screen -t srcds /usr/local/srcds_l/startserv )& > /dev/null
exit 0

For reference I am trying to start a Counter-Strike Source server, and startserv is the name of the C code which handles launching the server and collecting its output. Can anybody correct my syntax for that snippet, or tell me why its the wrong thing to do?

A: 

I don't have a complete solution for you, but you might want to look at nohup instead of screen. That eliminates the Expect need as well.

jkerian
A: 

That's a huge amount of overhead to communicate 1 bit of information ("start yourself now"). If you forget PHP then all the other subordinate requirements fall away.

If I was trying to do this, I'd probably write a perl script to listen on a TCP port for a "start" command and just fork off the process itself. This gets rid need for sh, expect, screen, and sudo.

msw
PHP kinda has to be there since the whole thing needs to be called by a button in a webpage submitting an AJAX reqest.. I kinda doubt that it still more efficient to have a whole extra listen daemon just to hear a VERY occasional TCP request from PHP. Of course you're correct that there is probably a lot of unnecessary overhead there. I suppose a fork would eliminate the need for screen, wouldn't it.
conartist6