Most recent edit on 2009-08-31 00:44:57 by JimLucas
Additions:
# Exit immediately if a command exits with a nonzero exit status.
echo "$DESC already running: PID# `cat $PIDFILE`"
exit 1
echo -n "Starting $DESC"
nohup $DAEMON 2>&1 1>/dev/null &
sleep 0.5
ps aux | grep $DAEMON | grep -v grep | awk -F' ' '{print $2}' > $PIDFILE
echo ". [`cat $PIDFILE`]"
fi
echo -n "Stopping $DESC"
kill `cat $PIDFILE`
rm $PIDFILE
echo "."
echo "$DESC is not running"
exit 1
d_start
;;
d_stop
;;
if [ -f $PIDFILE ]; then
echo "$DESC is running: PID# `cat $PIDFILE`"
else
echo "$DESC is not running"
fi
;;
# Meant to give you a way to "cleanup" the possible remains of a dead process
PID="`ps aux | grep $DAEMON | grep -v grep | awk -F' ' '{print $2}'`"
kill $PID
rm $PIDFILE
;;
restart|force-reload)
d_stop
sleep 0.5
d_start
;;
# Default action
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 1
;;
Basically, the above will start the $DAEMON script using the *nix command nohup (see references below). It then redirects STDOUT and STDERR to /dev/null. Then I use "&" to place the process into the background.
Deletions:
if [ -f $PIDFILE ]; then
echo "$DESC already running: PID# `cat $PIDFILE`"
exit 1
else
echo -n "Starting $DESC"
nohup $DAEMON 2>&1 1>/dev/null &
sleep 0.5
ps aux | grep $DAEMON | grep -v grep | awk -F' ' '{print $2}' > $PIDFILE
echo ". [`cat $PIDFILE`]"
fi
if [ -f $PIDFILE ]; then
echo -n "Stopping $DESC"
kill `cat $PIDFILE`
rm $PIDFILE
echo "."
else
echo "$DESC is not running"
exit 1
fi
start)
d_start
;;
stop)
d_stop
;;
status)
echo "$DESC is running: PID# `cat $PIDFILE`"
echo "$DESC is not running"
;;
cleanup)
PID="`ps aux | grep $DAEMON | grep -v grep | awk -F' ' '{print $2}'`"
kill $PID
rm $PIDFILE
;;
restart|force-reload)
d_stop
sleep 0.5
d_start
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 1
;;
Basically, the above will start the $DAEMON script using the *nix command nohup (see references below). It then redirects all output to /dev/null and then uses the "&" to place the process into the background.
Edited on 2009-08-30 23:01:00 by JimLucas
Additions:
References
nohup
Deletions:
nohup references:
Edited on 2009-08-30 23:00:22 by JimLucas
Additions:
Basically, the above will start the $DAEMON script using the *nix command nohup (see references below). It then redirects all output to /dev/null and then uses the "&" to place the process into the background.
nohup references:
Deletions:
Basically, the above will start the $DAEMON script using the *nix command nohup[1][2]. It then redirects all output to /dev/null and then uses the "&" to place the process into the background.
Edited on 2009-08-30 22:59:53 by JimLucas
Additions:
Basically, the above will start the $DAEMON script using the *nix command nohup[1][2]. It then redirects all output to /dev/null and then uses the "&" to place the process into the background.
http://www.cyberciti.biz/tips/nohup-execute-commands-after-you-exit-from-a-shell-prompt.html∞
http://www.idevelopment.info/data/Unix/General_UNIX/GENERAL_RunningUNIXCommandsImmunetoHangups_nohup.shtml∞
Deletions:
Basically, the above will start the $DAEMON script using the *nix command nohup∞. It then redirects all output to /dev/null and then uses the "&" to place the process into the background.
Oldest known version of this page was edited on 2009-08-30 22:54:23 by JimLucas []
Page view:
PHP daemon
First off, I need to say upfront, that this is not really a daemon at all. It looks like a daemon. It starts like a daemon. But it is missing many of the common daemon things...
Anyways, on to what I have created.
Lets start off by simply showing you what I have working.
#! /bin/sh
#
# tms_daemon
#
# Starts a listening daemon that acts as a Tribes Master Server
#
# Author: Jim Lucas <jlucas@cmsws.com>
#
# Version: 0.0.1
#
set -e
DESC="Tribes Master Server Daemon"
DAEMON=/var/www/domains/tribesmasterserver.com/scripts/tms-0.1.5.php
PIDFILE=/var/run/tms.pid
SCRIPTNAME=/var/www/domains/tribesmasterserver.com/scripts/tms_daemon
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
#
# Function that starts the daemon/service.
#
d_start() {
if [ -f $PIDFILE ]; then
echo "$DESC already running: PID# `cat $PIDFILE`"
exit 1
else
echo -n "Starting $DESC"
nohup $DAEMON 2>&1 1>/dev/null &
sleep 0.5
ps aux | grep $DAEMON | grep -v grep | awk -F' ' '{print $2}' > $PIDFILE
echo ". [`cat $PIDFILE`]"
fi
}
#
# Function that stops the daemon/service.
#
d_stop() {
if [ -f $PIDFILE ]; then
echo -n "Stopping $DESC"
kill `cat $PIDFILE`
rm $PIDFILE
echo "."
else
echo "$DESC is not running"
exit 1
fi
}
case "$1" in
start)
d_start
;;
stop)
d_stop
;;
status)
if [ -f $PIDFILE ]; then
echo "$DESC is running: PID# `cat $PIDFILE`"
else
echo "$DESC is not running"
fi
;;
cleanup)
PID="`ps aux | grep $DAEMON | grep -v grep | awk -F' ' '{print $2}'`"
kill $PID
rm $PIDFILE
;;
restart|force-reload)
d_stop
sleep 0.5
d_start
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
Basically, the above will start the $DAEMON script using the *nix command
nohup∞. It then redirects all output to /dev/null and then uses the "&" to place the process into the background.