Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - venox

Pages: [1]
1
Unix / Linux Discussion / Shell Script for Start/Stop Process
« on: August 23, 2005, 08:00:41 pm »
For a long time i used to start a process and then to kill the damn thing, id have to ps aux | grep process_name, then kill -9 PID.  It started to get pretty old after a while.. so... I wrote this little bit of shell script to start/stop services on my server(s).  I use it all over the place.

Code: [Select]

#!/bin/sh

# start
if [ "x$1" = "x" -o "x$1" = "xstart" ]; then
  /usr/local/bin/blah -FLAGS
# stop
elif [ "x$1" = "xstop" ]; then
ps wuax | grep PROCESS_NAME | awk '{system("kill -9 " $2)}'
fi


Usage:
1. create startstop.sh

2. chmod u+x startstop.sh

3. Change the "/usr/local/bin/blah -FLAGS" to whatever process you want to start.

4. Change the "PROCESS_NAME" to whatever the process name is.  Also, you want to make sure that your process does not spawn child processes.  If it does, add another line "ps wuax | grep PROCESS_NAME | awk '{system("kill -9 " $2)}'" for those child processes as well.

5. now you can ./startstop.sh start or ./startstop.sh stop

Fairly simple script, but it saves a lot of headache.

Also, I forgot to mention, this is a FORCED shutdown, NOT a friendly one.  So, be careful what you use it for.  It should not be used for large-scale processes like MySQL/Apache etc..

And as iago pointed out, sometimes instead of using the ps wuax method, killall -9 is better to use.  This is intended to kill generic processes, like, counterstrike servers and things of that nature.  Make certain that your PROCESS_NAME is not TOO! generic.

Also, if you want to restart a service, you can use

Code: [Select]
elif [ "x$1" = "xrestart" ]; then
kill -HUP PROCESS_NAME

Pages: [1]