Old 17th April 2011, 11:21   #1
marcm
Junior Member
 
Join Date: Apr 2011
Posts: 20
Creating, managing servers on Linux via PHP

Hello, I'm trying to figure out how to create, start and stop multiple Shoutcast server streams on my Linux server via PHP. I've been able to use a simple command like this:
system( "/shoutcasthome/sc_serv /shoutcasthome/sc_serv3.conf" ); to start a server.

However, I'm not sure how to stop this particular server from PHP. I need to get the PID I suppose, so I could run a kill on it.

I've seen the proc_open function, but I'm not exactly sure yet if this is the right approach. I think I could use proc_get_status to get the PID? But I'm not 100% sure. I can't quite figure out the proc_open function yet either.

Does anyone have any ideas or suggestions as to the best method for creating custom servers, starting and stopping them from PHP? Am I on the right track here?

Would it be better to have a c++ script and then control that via PHP? If I can just start and kill the individual server processes from PHP, that would be ideal.

Any help or pointers in the right direction would be very much appreciated! Thank you.
marcm is offline   Reply With Quote
Old 18th April 2011, 14:09   #2
MADxHAWK
Senior Member
 
Join Date: Jan 2010
Posts: 181
you can try to run the following shell-command via php:

code:
ps ux | awk '/sc_serv3.conf/ && !/awk/ {print $2}'


this command will return the pid of the shoutcast instance started with sc_serv3.conf configuration file in daemon mode. (Tested on a Debian Lenny x64 system.)

php Code could look something like that
PHP Code:
$config_file 'sc_serv3.conf';
$pid shell_exec("ps ux | awk '/" $config_file "/ && !/awk/ {print $2}'");
shell_exec("killall -9 $pid"); 
(not tested)

Hope it helps

PS: Allowing php to execute shell commands is always a security risk. You should add some security checks to the script and make sure that only the defined commands can be executed.

-MAD
MADxHAWK is offline   Reply With Quote
Old 23rd April 2011, 00:49   #3
marcm
Junior Member
 
Join Date: Apr 2011
Posts: 20
Hey Mad, thanks for taking the time to reply.

I came up with something before I saw your reply, not sure which would be better.

$pid = system('pgrep -u root -f sc_serv3.conf',$retval);

See any reason that this would cause problems?
marcm is offline   Reply With Quote
Old 23rd April 2011, 01:17   #4
jaromanda
Forum King
 
Join Date: Jun 2007
Location: Under the bridge
Posts: 2,289
I found by far the simplest method when working with multiple instances of sc_serv, was to symbolically link the executable to a unique name for each instance of sc_serv

e.g.

code:
ln -s sc_serv sc_serv_8000
ln -s sc_serv sc_serv_8002


etc

to run then

code:
sc_serv_8000 8000.conf
sc_serv_8002 8002.conf


etc

then to kill port 8000

code:
killall -9 sc_serv_8000


in my setup, I actually created a folder for each instance, keeping config, logs and the symbolically linked sc_serv separate for each instance ... which meant the config files had the same name a they were in a sub-folder, which simplified a lot of tasks, as all this was integrated into a phpbb3 forum

also, the start/stop (and other functions) were all done using a shell script

so the commands from php would be something like

code:
sc_control.sh start 8000
sc_control.sh stop 8000


the same script was periodically run from cron to make sure the servers were running, and to periodically rotate logs

the shell script allowed one to create and delete new instances, make changes to the config, speed limit per instance, all sorts of things, all done via the phpbb3 control panel, and therefore via PHP

unfortunately, the site I wrote all this for no longer exists, but I MAY have the code somewhere (I've upgraded my computer since then too)

"If you don't like DNAS, write your own damn system"

So I did
jaromanda is offline   Reply With Quote
Old 23rd April 2011, 12:23   #5
MADxHAWK
Senior Member
 
Join Date: Jan 2010
Posts: 181
There are always more then one way to get what you want. If this command works it would be ok to use.

If you work with form data, and you have to do, cause you need to select the instance to start or stop, make sure the data will be checked. So no one can execute any other command defined in your script. Also you should protect your script with a login, for example via .htaccess / .htpasswd

jaromanda sugesstion would be a good idea to make the script more save. Cause here you can work with the ports of the instance. You can hardcode all commands and only append the port number to them. Do a check, if the submittet data are an integer-value and if not, stop the script with throwing an errormessage. This will reduce the danger of an injection.


-MAD
MADxHAWK is offline   Reply With Quote
Old 28th April 2011, 03:03   #6
marcm
Junior Member
 
Join Date: Apr 2011
Posts: 20
Hey jaromanda and Mad, thanks for both of your replies. I will definitely be careful with any system commands and I always filter and test any sort of user input data to prevent any sort of injection.

Jaro, I really like your idea of symbolically linking each instance to a name, which I could then use to kill the instance without having to actually query to get the PID number. This is a noob question, but do the symbolically linked names become a file of any sort, or do they simply reside in memory? How do you then delete or remove the symbolic link? I also like your idea of keeping things in separate folders.. would be great for the log files.

So is the version 2 in beta stable enough to go into production with, in your opinion?

Thanks again for your help.
marcm is offline   Reply With Quote
Old 28th April 2011, 03:36   #7
jaromanda
Forum King
 
Join Date: Jun 2007
Location: Under the bridge
Posts: 2,289
symbolic links become an entry in a directory (just like files and directories and other things) ... though, they do not use up any hard disk space (besides the entry in the directory of course)

removing symbolic links is just a case of using the standard rm command

by the way, there are two types of link ... "hard" and symbolic ... in linux you create them using the ln command

ln [options] sourcefile destfile

where options includes -s for symbolic

there are differences between hard and symbolic links ... I'm not sure why I usually use symbolic for this sort of thing, but there was a reason

"If you don't like DNAS, write your own damn system"

So I did
jaromanda is offline   Reply With Quote
Old 22nd November 2011, 07:07   #8
marcm
Junior Member
 
Join Date: Apr 2011
Posts: 20
Well, instead of creating a new thread, I'll just tack this onto this older thread and hopefully get some feedback!

I have my PHP shoutcast manager almost finished, except I'm having a particular problem. ( I took a looong break from this and I'm back working on it now )

I'm creating a symbolic link of sc_serv and it's getting put into it's own folder. My big problem is that when I run a command from PHP to start the server, the server starts, but the script hangs.

I've tried the system, exec and shell_exec commands, such as:

$startitup = shell_exec("/shoutcasthome/8012/sc_serv_8012 /shoutcasthome/8012/sc_serv_8012.conf");

$startitup = system("/shoutcasthome/8012/sc_serv_8012 /shoutcasthome/8012/sc_serv_8012.conf",$returnval);

But nothing is letting the script both start the server and continue on.

So does anyone here have any clue as to what might be happening? I'm not sure if this is a problem due to it being a symbolic link or what. But I've been testing and testing and not getting anywhere. Very frustrating!
marcm is offline   Reply With Quote
Old 22nd November 2011, 07:51   #9
p0rt
Senior Member
 
Join Date: Nov 2010
Posts: 152
try exec( "cd /shoutcasthome/8012/ || /shoutcasthome/8012/sc_serv_8012 /shoutcasthome/8012/sc_serv_8012.conf" )

cd = whatever the command is on linux to change directory in terminal

it hangs on windows too, if the server is not installed as a service
p0rt is offline   Reply With Quote
Old 22nd November 2011, 08:26   #10
marcm
Junior Member
 
Join Date: Apr 2011
Posts: 20
Thanks p0rt, for the tip. However, I tried it and it still basically hangs or does nothing. I tried a semi-colon in there but that just made it hang as well. :/
marcm is offline   Reply With Quote
Old 22nd November 2011, 10:10   #11
p0rt
Senior Member
 
Join Date: Nov 2010
Posts: 152
exec() will probaly only work if you install sc_serv/trans as a deamon, then you can start the servers up, but finding the right pid to shut them down is rubbish in linux, if you are running multiple servers or transcoders

you will need to use $woutput = pass_thru() to start the deamon, then parse the $woutput to find the pid to be able to shut the deamon down, storing the pid in the database
p0rt is offline   Reply With Quote
Old 22nd November 2011, 23:45   #12
marcm
Junior Member
 
Join Date: Apr 2011
Posts: 20
Damn. There HAS to be a way to get this thing started with an alias/symbolic link via php without it hanging the script.
marcm is offline   Reply With Quote
Old 22nd November 2011, 23:58   #13
p0rt
Senior Member
 
Join Date: Nov 2010
Posts: 152
its no hardship to install servers as a deamon, you can use exec() to do that in php, the command line to use is in the docs/*.txt for each server

it has all been done in null nukes manager http://forums.winamp.com/showpost.ph...3&postcount=18
p0rt is offline   Reply With Quote
Old 23rd November 2011, 00:03   #14
thinktink
Forum King
 
thinktink's Avatar
 
Join Date: May 2009
Location: On the streets of Kings County, CA.
Posts: 3,013
Send a message via Skype™ to thinktink
code:
./sc_serv sc_serv.conf &
Try adding a space and an ampersand at the end of the shell execution command line. Also make sure screen logging is turned off in the configurations. The PID value that is outputed can also be redirected to a PID file for even more scripting fun.
thinktink is offline   Reply With Quote
Old 23rd November 2011, 02:03   #15
marcm
Junior Member
 
Join Date: Apr 2011
Posts: 20
I have ScreenLog=0 in the conf file that I use when I start up the server via the linked sc_serv_8012 - however, it appears that screen logging still happens as when I run it via SSH I can see it go into screen output mode basically. I wonder if there's another way to turn off the screen output?

I tried adding the ampersand and space and I tried using system and exec and no dice with that.

p0rt, I'm not sure yet how to install them as Daemons but I'll look into it...
marcm is offline   Reply With Quote
Old 23rd November 2011, 03:16   #16
marcm
Junior Member
 
Join Date: Apr 2011
Posts: 20
Well, I found a combination of commands that seems to work, which I found on the Ubuntu forum. Here's a link to the thread, for anyone who cares or any person stumbling upon this topic in the future.

http://ubuntuforums.org/showthread.php?t=1191270

Basically, I'm using the 'system' command in PHP. and then adding >/dev/null 2>&1 & at the end. All I get back in PHP as the return value is a 0, but that's sufficient. I did notice that the SSH output includes the PID, but I couldn't get that to show up in php - not as of yet anyway.

(I was prefixing it with nohup, but it doesn't seem to make a difference atm )

But all in all, this is great news, because I can now start and stop the server from php without it hanging up the script. I don't need the PID as long as I have the name of the symbolic link that I started.

Last edited by marcm; 23rd November 2011 at 05:04.
marcm is offline   Reply With Quote
Old 23rd November 2011, 14:54   #17
p0rt
Senior Member
 
Join Date: Nov 2010
Posts: 152
all php functions return true or false = 1 or 0, if you use pass_thru the command is executed and the process is ended, and php will return the processes window content to be used.. like

code:

2011-11-23 15:50:32 I msg:<***> Logger startup
2011-11-23 15:50:32 I msg:<***> version 2.0.0.29
2011-11-23 15:50:32 D msg:<***>
2011-11-23 15:50:32 W msg:[CONFIG] Invalid item on line 17
2011-11-23 15:50:32 W msg:[CONFIG] Invalid item on line 18
2011-11-23 15:50:32 W msg:[CONFIG] Invalid item on line 1
2011-11-23 15:50:32 W msg:[CONFIG] Invalid item on line 2
2011-11-23 15:50:32 I msg:********************************************
*******************
2011-11-23 15:50:32 I msg:** SHOUTcast Distributed Network Audio Serve
r (DNAS)
2011-11-23 15:50:32 I msg:** Copyright (C) 1999-2011 Nullsoft, Inc. A
ll Rights Reserved.
2011-11-23 15:50:32 I msg:** Use "sc_serv filename.conf" to specify a
config file.
2011-11-23 15:50:32 I msg:********************************************
*******************
2011-11-23 15:50:32 I msg:[SHOUTcast] DNAS/win32 v2.0.0.29 (Jul 31 201
1) starting up...
2011-11-23 15:50:32 I msg:[MAIN] PID: 3596
2011-11-23 15:50:32 I msg:[MAIN] Loaded config from sc_serv_local.conf

2011-11-23 15:50:32 I msg:[MAIN] Calculated CPU count is 1



you need to get the pid from this line : 2011-11-23 15:50:32 I msg:[MAIN] PID: 3596

to run a process, when php has ended, you need a bat or symlink, so the bat or symlink get executed, which executes the program as a seperate process, and php will end the bat or symlink process

if you are going to run multiple servers or transcoder for each stream id, its better to use windows and run them as services
p0rt is offline   Reply With Quote
Old 23rd November 2011, 19:01   #18
marcm
Junior Member
 
Join Date: Apr 2011
Posts: 20
p0rt,

Thanks for the info. I tried using pass_thru last night and I had no luck with it. I couldn't get it to start the server at all.

I am using symlinks and I can now start and stop the server based upon the symlink name, so as of now I don't need the PID. But if I can get it returned back to php (with the initial startup command), I wouldn't mind having it. I know how I can fetch it separately but it would be nice to have it returned with exec or system.

I'll be testing the setup and starting of multiple servers soon.

Thanks again for your reply, much appreciated.
marcm is offline   Reply With Quote
Old 24th November 2011, 12:21   #19
p0rt
Senior Member
 
Join Date: Nov 2010
Posts: 152
too much hassle for a opensource portal, linux can get deamon names and catch up with windows, linux only has chmod and nothing else good anymore

while linux distros have all been swapping desktops, windows have taken over with backend stuff
p0rt is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Shoutcast > Shoutcast Technical Support

Tags
manage, php, proc_open, shoutcast, system

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump