Friday, October 12, 2007

Linux generic start and stop scripts

If you are following my twitter account, you will notice that I recently switched to Ubuntu and will never look back. Sometimes I find myself having to create start and stop scripts for weblogic, glassfish, hudson, etc. All these scripts are generic enough that you just copy and paste and modify some names. But I don't do it frequently enough to have memorized the syntax. Therefore I want to document and explain start and stop scripts that are generic enough they can be used for almost anything.

Next you will see the exact start and stop scripts we use in our Continuous Integration environment to manage hudson.

Start Script - start-hudson.sh
#!/bin/bash
nohup nice java -jar hudson-1.136.war --httpPort=8282 > ./hudson.out 2>&1 &

Stop Script - stop-hudson.sh
#!/bin/bash
kill `ps -ef | grep hudson-1.136.war | grep -v grep | awk '{ print $2 }'`

Start Script explained
nohup nice
I don't claim to be an advanced linux user, so don't expect a master's thesis on this one. Nohup on Wikipedia tells us that nohup "is a unix command that is used to run another command while suppressing the action of the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out. It is most often used to run commands in the background and output that would normally go to the terminal goes to the file called nohup.out if it is not redirected". Nice allows you to run a program with a modified scheduling priority with -20 being most important and 20 being less important. I believe the default when using nice without a value is 0.

The '>' command
Takes the output from my java command and places it in the file ./hudson.out.

The '2>&1' command
2> is standard error and &1 is standard output. So combined it says take standard error and put it in standard output, which the previous command said take standard output and put it in ./hudson.out. So together these two commands put standard output and error in ./hudson.out.

The '&' command
When appending it at the end like it is, it means runs this process in the background.

Stop Script explained
Uses the Process Status (ps) command and grep to search for a process that includes the text 'hudson-1.136.war'. Then it uses awk to split out the output from ps to get the Process ID. Once the Process ID is found it passes it to the kill program to stop the daemon (pipes rock!). This method of using kill to stop a process is rather brute force and does have some flaws if you don't use unique text to search (multiple users running hundreds of programs). Most programs, say weblogic, for example should already have a stop script, so obviously prefer those over my example.

So what if you want the startup script to begin at boot time? Well, add a soft link pointing to your startup script (ln -s /home/jlorenzen/bin/start-hudson.sh) in /etc/init.d/. Then use the update-rc.d command to add it at boot time (update-rc.d softlink_name defaults).

Update: Do to this posts popularity, I created a github account and uploaded these scripts so they can be easily shared, referenced, and forked. See http://github.com/jlorenzen/linux-config-files/tree for more information.

12 comments:

jlorenzen said...

This was just an example of how to create generic linux start and stop scripts. Hudson now supports linux packages; ubuntu for example. And it's even easier to get started using hudson: sudo apt-get install hudson. This installs hudson as a service which starts on a reboot.

Unknown said...

A few tips:
1. About the simple pattern:
ps -ef | grep hudson-1.136.war | grep -v grep
You can prevent grep from matching its own ps by wrapping the first char in square brackets like so:
ps -ef | grep [h]udson-1.136.war
2. Any time you see yourself using grep in a pipe with awk or sed, stop, simplify. This takes 2 greps out of your pipe:
ps -ef | awk '/[h]udson-1.136.war/{ print $2 }'

Both awk and sed can do their own regex filtering, no need to bring grep to the party.

jlorenzen said...

@Richard,
Thanks. Those are excellent suggestions.

Kamagra said...

And it's even easier to get started using hudson: sudo apt-get install hudson. This installs hudson as a service which starts on a reboot.

newbie said...

Silly article; hudson is started alright when a run level is entered, but is never stopped, because the stop script was never called from anywhere. So you got plenty of hudson instances running after switching the level around a few times.

clarion hotel anaheim ca said...

Great piece... you did great job bro keep it up. Thanks for a great time visiting your site.

Ravindra Singh Gohil said...

HI jlorenzen,

Thanks a lot for the start & stop scripts, it works great. Hey instead of getting output in a text file, can we get that in a terminal. If it is possible it would really help me.

Thanks in advance,
Ravindra Gohil

Ravindra Singh Gohil said...

Hey do have any idea about restarting a script (containing .jar file) with a given time interval (daemon process).
I will appreciate any input regarding to this from your side.


Thanks & Regards,
Ravindra Gohil

Anonymous said...

Thanks a million! You just made my day.

Anonymous said...

Found out recently:
pkill 'somestring'
Is on most OSes and easier than the ps|kill
Also it can stop all processes, if you accidently started more than one

Generic viagrabuy said...

the way you present the things is really good please keep sharing such a things
Generic Viagra

generic viagra 100mg said...

Thanks for the blog