Author Topic: Shell Script for Start/Stop Process  (Read 3693 times)

0 Members and 1 Guest are viewing this topic.

Offline venox

  • Newbie
  • *
  • Posts: 16
  • RAWR!
    • View Profile
    • Shiver7
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
« Last Edit: August 23, 2005, 09:09:18 pm by venox »

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Shell Script for Start/Stop Process
« Reply #1 on: August 23, 2005, 08:11:50 pm »
How is this different from "killall -9 <name>"? :)

Offline venox

  • Newbie
  • *
  • Posts: 16
  • RAWR!
    • View Profile
    • Shiver7
Re: Shell Script for Start/Stop Process
« Reply #2 on: August 23, 2005, 08:29:36 pm »
How is this different from "killall -9 <name>"? :)

The only difference is that using the "ps wuax" method is generic.  It is probably BETTER in a lot of cases to use killall -9, but sometimes I like to make sure all of the processes are found.  The above method shouldn't be used on things like.... apache or mysql.

For instance, Wine has "wineserver" and "wine".  If I want to kill wine, I use ps wuax | grep wine | awk '{system("kill -9 " $2)}'.  Instead of killing each individually.  It's me being lazy :)

Also, I use these scripts in one central location on the server, to start/stop services.  Rather than having to move about the server, I can start and stop all the services from my rc.d folder.

For services which offer their own start/stop method(s).  I just use their start/stop, but from this script.  Because, as I said, I put all of these in one folder on the machine.  It makes it a lot easier for me personally.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Shell Script for Start/Stop Process
« Reply #3 on: August 23, 2005, 08:33:21 pm »
How is this different from "killall -9 <name>"? :)

The only difference is that using the "ps wuax" method is generic.  It is probably BETTER in a lot of cases to use killall -9, but sometimes I like to make sure all of the processes are found.  The above method shouldn't be used on things like.... apache or mysql.

For instance, Wine has "wineserver" and "wine".  If I want to kill wine, I use ps wuax | grep wine | awk '{system("kill -9 " $2)}'.  Instead of killing each individually.  It's me being lazy :)

Also, I use these scripts in one central location on the server, to start/stop services.  Rather than having to move about the server, I can start and stop all the services from my rc.d folder.

For services which offer their own start/stop method(s).  I just use their start/stop, but from this script.  Because, as I said, I put all of these in one folder on the machine.  It makes it a lot easier for me personally.

What happens if you're running a life critical application called "wineisyummy", which has nothing to do with the Wine that Is Not an Emulator?  You'll kill it!


Offline venox

  • Newbie
  • *
  • Posts: 16
  • RAWR!
    • View Profile
    • Shiver7
Re: Shell Script for Start/Stop Process
« Reply #4 on: August 23, 2005, 08:38:56 pm »
How is this different from "killall -9 <name>"? :)

The only difference is that using the "ps wuax" method is generic.  It is probably BETTER in a lot of cases to use killall -9, but sometimes I like to make sure all of the processes are found.  The above method shouldn't be used on things like.... apache or mysql.

For instance, Wine has "wineserver" and "wine".  If I want to kill wine, I use ps wuax | grep wine | awk '{system("kill -9 " $2)}'.  Instead of killing each individually.  It's me being lazy :)

Also, I use these scripts in one central location on the server, to start/stop services.  Rather than having to move about the server, I can start and stop all the services from my rc.d folder.

For services which offer their own start/stop method(s).  I just use their start/stop, but from this script.  Because, as I said, I put all of these in one folder on the machine.  It makes it a lot easier for me personally.

What happens if you're running a life critical application called "wineisyummy", which has nothing to do with the Wine that Is Not an Emulator?  You'll kill it!



Haha, that is true, but, I know what processes run on my machine.  So, I am very concious about what I grep for.  As I said above, depending on the processes, I'll use 1 of 3 things in the above shellscript.

1. ps wuax
2. killall -9
3. kill -HUP

I use all of the above.

Offline mynameistmp

  • Moderator
  • Full Member
  • *****
  • Posts: 111
  • Hi! I'm new here!
    • View Profile
Re: Shell Script for Start/Stop Process
« Reply #5 on: August 24, 2005, 03:20:33 am »
You should take a look at some of the more modern process manipulation packages. Probably the best known package would be procps, the package that includes top.

Check out the man file for the command skill (pkill is essentially the same thing only with more formal parameters to eliminate any ambiguities). You can send signals to process by name, terminal, username, or PID. Example:
This would freeze the user on terminal pts/2:
Quote
# skill -STOP pts/2

 To wake him back up:
Quote
# skill -CONT pts/2

If you wanted to kill all of user tmp's bash processes:
Quote
# pkill -KILL -u tmp bash

Lots of people don't think to use pgrep to return a list of PIDs matching the process name. This will eliminate a step for you ;P
Quote
sh-3.00$ pgrep xmms
1600
1601
1602
1605
10422
10423

Offline venox

  • Newbie
  • *
  • Posts: 16
  • RAWR!
    • View Profile
    • Shiver7
Re: Shell Script for Start/Stop Process
« Reply #6 on: August 24, 2005, 06:08:55 am »
You should take a look at some of the more modern process manipulation packages. Probably the best known package would be procps, the package that includes top.

Check out the man file for the command skill (pkill is essentially the same thing only with more formal parameters to eliminate any ambiguities). You can send signals to process by name, terminal, username, or PID. Example:
This would freeze the user on terminal pts/2:
Quote
# skill -STOP pts/2

 To wake him back up:
Quote

# skill -CONT pts/2

If you wanted to kill all of user tmp's bash processes:
Quote
# pkill -KILL -u tmp bash

Lots of people don't think to use pgrep to return a list of PIDs matching the process name. This will eliminate a step for you ;P
Quote
sh-3.00$ pgrep xmms
1600
1601
1602
1605
10422
10423


I use top all the time, but not any of this other stuff.  Thanks for the suggestion(s) :)  I'll make sure I take a gander at that stuff.