Skip site navigation (1)Skip section navigation (2)
Date:      04 Dec 2001 18:19:23 +0100
From:      =?ISO-8859-1?Q?S=F8ren?= Neigaard <neigaard@e-box.dk>
To:        jconner@enterit.com
Cc:        freebsd-questions <freebsd-questions@FreeBSD.ORG>
Subject:   Re: How to make a start/stop/restart script
Message-ID:  <1007486364.2236.11.camel@lapdancer.dk>
In-Reply-To: <200111271313.AA4194478@enterit.com>
References:  <200111271313.AA4194478@enterit.com>

next in thread | previous in thread | raw e-mail | index | archive | help

Looong time, no time :) But now I have the time to work with this again.

I tried to fool around on my Linux box, but I cant seem to understand
this ps|grep thing. Sometimes when I do a "ps -aux" I get some Java
processes with the string "mailsmsgateway" in them, but if I do a "ps
-aux|grep mailsmsgateway" I get nothing?? Why?

/Søren

On Tue, 2001-11-27 at 19:13, jconner wrote:
> 
> ---------- Original Message ----------------------------------
> From: Søren Neigaard <neigaard@e-box.dk>
> Date: 27 Nov 2001 18:57:11 +0100
> 
> >Sorry for the offtopic mail, but I know you guys know the answer :)
> 
> This isn't really off-topic :)
> 
> >
> >How do I make a script that can start/stop/restart a Java process?
> >Somting about probing the process string, but how do I do it!?
> >
> >It has to work on both Unix and Linux too :)
> 
> I won't go into the specifics of it but it is not that difficult to do.  Your questions more like you're interested in knowing how to kill the process vs starting the process.  The other thing is that you are wanting it to work on multiple *nix boxen which means a lil bit more work (but not much).
> 
> First, you need to learn case...esac statements.
> 
> case $1 in
>    start) do something here;;
>    stop)  do something here;;
>    *) default message (usually an echo to STDOUT for usage)
> esac
> 
> You will also want to use the case..esac statement for the multiple OS's...
> 
> case $(uname -s) in
>    FreeBSD) psargs="u $LOGNAME";;
>    SunOS)   psargs="-f -u $LOGNAME"
>    *) exit 10;;
> esac
> 
> Once that is set then in you stop) section of the first case statement you can call the ps with the $psargs set in the second case statement.
> 
> Here, let me wrap this up with a small example using what I have up there.
> 
> <code>
> # Start/Stop script (simple and unfinished)
> # Example by Jim Conner (MJ, TN;CHT, NC;LA, CA)
> 
> funcUsage() {
>     echo "ERROR: $1"
>     cat << EOM
> Usage: $shProgName [start|stop|etc etc]"
> 
> explanation
> EOM
> }
> 
> service="/usr/sbin/somebinary"
> config="/etc/someconfig"
> 
> case $(uname -s) in
>    FreeBSD) psargs="u $LOGNAME";;
>    SunOS)   psargs="-f -u $LOGNAME";;
>    *) exit 10;;
> esac
> 
> case $1 in
>    start) [ ! -x $service ] && funcUsage "Can't find $service or its not executable";;
>    stop)  pid=$(ps $psargs | \
>           grep $(basename $service) | \
>           grep -v grep | \
>           awk '{print $2}') # usually the second field, season to taste
> 
>           kill -9 $pid;;
>    *) funcUsage " Unknown command: $1"
> esac
> 
> </code>
> 
> This code is NOT complete nor is it tested but it gives a simple idea about what to do (I hope).  Check out the other run scripts in /usr/local/etc/rc.d/.  Those may have some healthy ideas.
> 
> - Jim
>    stop)  ps $psargs | grep -v ;;
> 
> >-- 
> >..............................
> >Med venlig hilsen/Best regards
> >Søren Neigaard
> >Registered Linux User #239437
> >
> >
> >To Unsubscribe: send mail to majordomo@FreeBSD.org
> >with "unsubscribe freebsd-questions" in the body of the message
> >
> 
> 
-- 
..............................
Med venlig hilsen/Best regards
Søren Neigaard
Registered Linux User #239437


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1007486364.2236.11.camel>