Link to home
Start Free TrialLog in
Avatar of Brad_nelson1
Brad_nelson1

asked on

making a application a service in linux

I need to find the BEST way to run a app as a service in linux.

Im going to run a php script that will call that app and start or stop it. We currently do this in windows using the net stop service_name and we need a similar way in linux. Something like firedaemon but for linux.

The solution should meet the following requirements:

1.) be able to start,stop,restart the service from a php script via the internet.

2.) be a stable solution.

3.) run any type of app i choose to run this way.


Thanks for any help you can provide!
ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brad_nelson1
Brad_nelson1

ASKER

Well I have learned about the utility called screen, this will create a virtual session that runs the app.  I have it working so now all i have to do is kill the pid that the username is running.

Does anyone know how i would go about matching the PID with the username so that i can kill that pid?


ps aux | grep username | awk '{ print $2; }'

The above command will give you the pid's of all the processes run by the user: 'username'
To eliminate the last 4 lines from the above output:

ps aux | grep username | awk '{ print $2; }' | head -n-4

If you want to kill all the processes listed by the above command:

ps aux | grep username | awk '{ print $2; }' | head -n-4 | xargs kill -9

You can get the list of pid's of the processes you are running by replacing "username" in the above command with program name.
im running Red Hat Linux, it appears some of those commands are invalid, because they the bottom line doest run correctly. Is that code be compatible for RH?
Those commands should work fine in RH. I'm personally using RH. But if the last line [ps aux | grep username | awk '{ print $2; }' | head -n-4 | xargs kill -9] does not work for you, maybe xargs is not available in your system. Try this instead:
===========
for processid in `ps aux | grep username | awk '{ print $2; }' | head -n-3`; do  kill -9 $processid; done
============
NB: note the apostrophe's in the first line. The one just before 'ps' is usually the key just below ESC. The one inside is the other one. You may copy the code exactly to your shell.

If head don't work for you, omit it. That shouldn't be much problem:
===========
for processid in `ps aux | grep username | awk '{ print $2; }' `; do  kill -9 $processid; done
============

If awk is a problem, try:
===========
for processid in `ps aux | grep username | tr -s ' ' | cut -d ' ' -f 2`; do  kill -9 $processid; done
============
NB: There's a space between the internal apostrophe's. You may copy the code exactly to your shell.