Script per crear dimonis amb shell-bash script (provat amb GNU/Linux).
#!/bin/bash
#
# Author: Sergi Coll <sergi@nit.cat>, 2011
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
### BEGIN INIT INFO
# Provides: scs
# Required-Start: $network $syslog
# Should-Start: ssh
# Required-Stop: $network $syslog
# Should-Stop: sshd
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: BASH DAEMON TEST
# Description: BASH DAEMON TEST
### END INIT INFO
DAEMONNAME=`basename $0`
PIDDIR=/var/run/
PIDFILE=scsdaemon.pid
TIMER=15
LOGDIR=/var/log/
LOGFILE=$DAEMONNAME.log
DATA=`date +"%h %d %H:%M:%S"`
touch -a $LOGDIR$LOGFILE 2>/dev/null
if [ "$?" != 0 ]; then
echo "ERROR: Problems with Log $LOGDIR$LOGFILE. Exiting..."
exit 2
fi
###########################################################################
#DAEMON FUNCTIONS
###########################################################################
function logprint {
DATA=`date +"%h %d %H:%M:%S"`
echo "$DATA $HOSTNAME $0[$PID] : $1" >> $LOGDIR$LOGFILE
}
###########################################################################
#DAEMON MAIN
###########################################################################
case "$1" in
start)
echo "Starting $0 Daemon... "
if [ -d "$PIDDIR" ]; then
if [ -e "$PIDDIR$PIDFILE" ]; then
echo "ERROR: PID file exists perhaps daemon is running"
logprint "ERROR: PID file exists perhaps daemon is running"
exit 2
fi
else
echo "ERROR: PID file directory don't exists"
logprint "ERROR: PID file directory don't exists"
exit 2
fi
(
PID=$BASHPID
echo $BASHPID >$PIDDIR$PIDFILE
if [ ! -e "$PIDDIR$PIDFILE" ]; then
echo "ERROR: creating PID file, check perminissions."
logprint "ERROR: PID file directory don't exists"
exit 2
fi
logprint "Starting $0 Daemon ..."
while :; do
sleep $TIMER;
done
) & disown -h $!
;;
stop)
echo "Stopping $0 Daemon... "
logprint "Stopped $0 Daemon ..."
if [ ! -e "$PIDDIR$PIDFILE" ]; then
echo "ERROR: PID file don't exists"
logprint "ERROR: PID file don't exists"
exit 2
fi
PID=`cat $PIDDIR$PIDFILE`
kill $PID
ps -p $PID >/dev/null
if [ "$?" == 0 ]; then
echo "ERROR: process exists"
logprint "ERROR: process exists"
exit 1
fi
rm -f $PIDDIR$PIDFILE
logprint "Stopped Daemon ..."
;;
force-restart)
rm -f $PIDDIR$PIDFILE
$0 start
;;
restart)
$0 stop
$0 start
;;
status)
if [ ! -e "$PIDDIR$PIDFILE" ]; then
echo "ERROR: PID file don't exists, perhaps don't running"
exit 2
fi
PID=`cat $PIDDIR$PIDFILE`
ps -p $PID
if [ "$?" != 0 ]; then
echo "ERROR: PID file exists but process no."
echo "Removing PID file"
rm -f $PIDDIR$PIDFILE
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|status|force-restart|restart}"
exit 1
;;
esac