|
|
|
|
|
|
| Author |
Message |
Kaitlyn Luna *nix forums beginner
Joined: 18 May 2006
Posts: 17
|
Posted: Wed Jul 19, 2006 5:29 am Post subject:
rc.d shell script
|
|
|
I tried to follow the Handbook's example in 11.5. But I also don't want
to have multiple instances of this program running. So please review my
first attempt for (in)correctness before I turn it loose for real.
#!/bin/sh
case "$1" in
start)
if test -n `ps acx | grep foo | grep -v grep | cut -b 1-6` ; then
echo "foo is already running" >&2
exit 1
else
echo -n ' foo'
/usr/local/bin/foo -bar
fi
;;
stop)
kill -9 `ps acx | grep foo | grep -v grep | cut -b 1-6`
;;
*)
echo "Usage: `basename $0` {start|stop}" >&2
exit 64
;;
esac
exit 0 |
|
| Back to top |
|
 |
Vlad Mfk *nix forums beginner
Joined: 19 Jul 2006
Posts: 7
|
Posted: Wed Jul 19, 2006 5:48 am Post subject:
Re: rc.d shell script
|
|
|
Kaitlyn Luna wrote:
| Quote: | I tried to follow the Handbook's example in 11.5. But I also don't want
to have multiple instances of this program running. So please review my
first attempt for (in)correctness before I turn it loose for real.
#!/bin/sh
case "$1" in
start)
if test -n `ps acx | grep foo | grep -v grep | cut -b 1-6` ; then
echo "foo is already running" >&2
exit 1
else
echo -n ' foo'
/usr/local/bin/foo -bar
fi
;;
stop)
kill -9 `ps acx | grep foo | grep -v grep | cut -b 1-6`
;;
*)
echo "Usage: `basename $0` {start|stop}" >&2
exit 64
;;
esac
exit 0
|
Looks fine. What's the question ? |
|
| Back to top |
|
 |
Kaitlyn Luna *nix forums beginner
Joined: 18 May 2006
Posts: 17
|
Posted: Wed Jul 19, 2006 6:44 am Post subject:
Re: rc.d shell script
|
|
|
Vlad Mfk <freelance@voliacable.com> wrote in news:e9kh3i$21uv$1
@behemoth.volia.net:
| Quote: | Looks fine. What's the question ?
|
With it being my first attempt, I wanted some feedback on how I did before
I let it run as root during startup.
Turns out it doesn't work as expected I get the already running error
message when the program isn't running and should be started. |
|
| Back to top |
|
 |
Keve Nagy *nix forums Guru Wannabe
Joined: 21 Feb 2005
Posts: 250
|
Posted: Wed Jul 19, 2006 12:01 pm Post subject:
Re: rc.d shell script
|
|
|
Kaitlyn Luna wrote:
| Quote: | Turns out it doesn't work as expected I get the already running error
message when the program isn't running and should be started.
|
Hi,
Why not use a pid file? That is usually how other similar scripts work.
You don't need to check for an already running instance in the rc
script, keep it simple. You do the check in the real script (your
/usr/local/bin/foo).
Have it create a pid file in /var/run/foo.pid when it starts, and also
check if the pid file exists already. If the pid file exists, there is
an already running instance, so you exit immediately from this new one.
<example>
#!/bin/sh
LOCKFILE="/var/run/foo.pid"
trap 'rm $LOCKFILE; exit 2' 1 2 3 15
if [ -f $LOCKFILE ]
then
echo "An instance is already running!"
exit 1
else
echo $$ > $LOCKFILE
fi
#
# Whatever your script is intended to do should come below.
#
#
# This is the end of your code. Exiting normally from the script.
#
rm $LOCKFILE
exit 0
</example>
The shell variable $$ contains your current process id. Therefore the
line "echo $$ > $LOCKFILE" will write the PID to the pid file.
This can also be used later to kill the running instance by:
kill -HUP `cat /var/run/foo.pid`
for example.
As you can see, the script will delete its pid file every time when it
exits normally (the final 2 lines).
There is also a line in the beginning that makes sure the pid file will
also be deleted if the running instance ends abnormally, receiving a
signal that forces it to quit.
trap 'rm $LOCKFILE; exit 2' 1 2 3 15
This traps all the relevant signals:
1 = SIGHUP, a "kill -HUP"
2 = SIGINT, a Ctrl+C
3 = SIGQUIT, a Ctrl+\
15 = SIGTERM, a normal "kill" or a "shutdown"
and when any of these are received by your script, the pid file will be
deleted and the script exits with an exit code of 2.
Remember though, that signal 9 (SIGKILL, a "kill -9" or "kill -KILL")
cannot be trapped. So even if you place a 9 into that "trap" line, it
will not handle the SIGKILL and the script will stop without deleting
the pidfile. In that case, you will need to remove the pid file manually
before you can start your script again.
I hope these will help!
Regards,
Keve
--
if you need to reply directly:
keve(at)mail(dot)poliod(dot)hu |
|
| Back to top |
|
 |
Kaitlyn Luna *nix forums beginner
Joined: 18 May 2006
Posts: 17
|
Posted: Wed Jul 19, 2006 2:54 pm Post subject:
Re: rc.d shell script
|
|
|
Keve Nagy <see_my_sig@mail.invalid> wrote in
news:4i6ldeF2baakU1@individual.net:
| Quote: | Why not use a pid file? That is usually how other similar scripts
work. You don't need to check for an already running instance in the
|
I almost did, but then realized a potential problem. What about a power
failure? Unless FreeBSD clears /var/run earlier in the boot process, the
..pid file could stop the program from running when I start up after power
is restored. Then I'd have to remove the .pid file and start manually --
and remember to do so in the first place. My aim was to eliminate manual
intervention. |
|
| Back to top |
|
 |
Steve O'Hara-Smith *nix forums Guru
Joined: 09 Mar 2005
Posts: 327
|
Posted: Wed Jul 19, 2006 3:35 pm Post subject:
Re: rc.d shell script
|
|
|
On Wed, 19 Jul 2006 14:01:22 +0200
Keve Nagy <see_my_sig@mail.invalid> wrote:
| Quote: | Kaitlyn Luna wrote:
Turns out it doesn't work as expected I get the already running error
message when the program isn't running and should be started.
Hi,
Why not use a pid file? That is usually how other similar scripts work.
You don't need to check for an already running instance in the rc
script, keep it simple. You do the check in the real script (your
/usr/local/bin/foo).
Have it create a pid file in /var/run/foo.pid when it starts, and also
check if the pid file exists already. If the pid file exists, there is
an already running instance, so you exit immediately from this new one.
|
It is usually a good idea to read the pid file and check to see if
the process is actually running too:
| Quote: | #!/bin/sh
LOCKFILE="/var/run/foo.pid"
trap 'rm $LOCKFILE; exit 2' 1 2 3 15
if [ -f $LOCKFILE ]
then
OLDPID=`cat $LOCKFILE` |
if kill -0 $OLDPID
then
| Quote: | echo "An instance is already running!"
exit 1
|
fi
fi
Skip the else - if the thing is already running you will have exited.
| Quote: | echo $$ > $LOCKFILE
|
So this fi moves up
| Quote: | fi
There is also a line in the beginning that makes sure the pid file will
also be deleted if the running instance ends abnormally, receiving a
signal that forces it to quit.
|
Well some of them anyway :)
| Quote: | the pidfile. In that case, you will need to remove the pid file manually
before you can start your script again.
|
Checking for the process existing is a good way of handling the
edge cases.
--
C:>WIN | Directable Mirror Arrays
The computer obeys and wins. | A better way to focus the sun
You lose and Bill collects. | licences available see
| http://www.sohara.org/ |
|
| Back to top |
|
 |
Keve Nagy *nix forums Guru Wannabe
Joined: 21 Feb 2005
Posts: 250
|
Posted: Wed Jul 19, 2006 6:56 pm Post subject:
Re: rc.d shell script
|
|
|
Kaitlyn Luna wrote:
| Quote: | I almost did, but then realized a potential problem. What about a power
failure? Unless FreeBSD clears /var/run earlier in the boot process, the
.pid file could stop the program from running when I start up after power
is restored. Then I'd have to remove the .pid file and start manually --
and remember to do so in the first place. My aim was to eliminate manual
intervention.
|
Sure, I had that problem too. This is where the additional checking
comes in, so that when the script is started and the pid file already
exists,
1., Get the process ID from the pid file!
2., See if a running process exists with that pid!
If NO, you can safely remove the pid file and create a new one.
(You just had your power failure! )
3., If the process with that PID is not "foo", do as with step #2!
Otherwise an instance of your script is really running.
There you are. All set. Maximum safety. :-)
Good luck!
Keve
--
if you need to reply directly:
keve(at)mail(dot)poliod(dot)hu |
|
| Back to top |
|
 |
Kaitlyn Luna *nix forums beginner
Joined: 18 May 2006
Posts: 17
|
Posted: Thu Jul 20, 2006 8:34 pm Post subject:
Re: rc.d shell script
|
|
|
Here's what I've got so far. Am I getting close to getting it right?
#!/bin/sh
LOCKFILE="/var/run/foo.pid"
trap 'rm $LOCKFILE; exit 2' 1 2 3 15
StartDaemon ( ) {
echo 'Starting foo...'
/usr/sbin/foo -user quistis
echo $$ > $LOCKFILE
echo 'Started'
exit 0
}
case "$1" in
start)
if [ -f $LOCKFILE ] ; then
# There is a PID file, check it out
if `cat $LOCKFILE` != `ps acx | cut -b 1-6 | grep $LOCKFILE` ; then
# The PID not in use, probable power failure or reset button
rm $LOCKFILE # Remove stale lockfile
StartDaemon # Clear to go
fi
if `ps acx | grep $LOCKFILE` != `ps acx | grep foo` ; then
# Someone else has the PID
rm $LOCKFILE # Remove stale lockfile
StartDaemon # Clear to go
fi
# Fall-through after tests
echo "An instance is already running!" >&2
exit 1
else
# No old PID file, clear to go
StartDaemon
fi
;;
stop)
kill `cat $LOCKFILE`
rm $LOCKFILE
echo "Stopped."
;;
*)
echo "Usage: `basename $0` {start|stop}" >&2
exit 64
;;
esac |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Thu Nov 20, 2008 11:11 pm | All times are GMT
|
|
Masini second hand | Debt | Mortgages | Free Cingular Ringtones | Buy Anything On eBay
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|