As noted in
UnixShellPatterns daemons need to be isolated from their terminals.
Forces
One problem with daemons is stopping them. Another is avoiding running two
of them at one time. You also need to be able to stop and restart a daemon
without thinking.
Solution
A simple solution is
- Make the daemon generate its own stop script.
- Write a start script
You can use the data in the stop script to test whether the daemon is still running.
Example
I run a simple Mailing list for students in some of my classes. For
historical reasons it is called 'bbs'. Once upon a time
The crontab was a bit disfunctional on the system so I used a daemon was called
bbs.daemon
I would leave it running, sorting my Email, and redistributing it to
other people in the class:
: script of daemon that sorts bbs messages
echo kill -9 $$ > /u/faculty/dick/bin/stop.bbs
while sleep 30
do
/u/faculty/dick/bin/post.bbs
done
When I run start.bbs the script checks to see if the
daemon is already running, and if not spawns the
deamon:
: script to start an agent that proceses personal and BBS mail
cd $HOME
default=180
if /u/faculty/dick/bin/psg bbs|egrep bbs.daemon
then
echo is already running
/u/faculty/dick/bin/psg bbs|egrep bbs.daemon|awk '{print $2}'|
sed 's/^/kill -9 /' > /u/faculty/dick/bin/stop.bbs
exit 1
fi
pwd
echo Starting Bulletin Board with delay ${1:-$default} seconds....
cat /dev/null >nohup.out
nohup nice bbs.daemon ${1:-$default} &
sleep 1; echo
(psg is another script that does a PS and a Grep).
--
DickBotting
any comments?
I've seen a somewhat different approach (Linux's gpm mouse daemon), where the program is written so that it check for an instance of itself running, and if found, exits, unless invoked with a specific argument (-k), where if there is another running copy, kills that copy before exiting. --
PeteHardie
CategoryUnixShellPattern