Here's an example systemd script for Spiffy the webserver, for use on systems using the systemd daemon manager.
Systemd requires two files to be placed in two different locations:
- A plain text script file named "spiffy"
- A plain text service file named "spiffy.service"
Be sure to make both files executable and owned by root.
The spiffy file, to be placed in /usr/lib/systemd/scripts/
#! /bin/sh
# -*- shell-script -*-
# This is /etc/init.d/spiffy on Linux systems using systemd
# It assumes you have a "spiffy.scm" script in /usr/local/libexec and a log directory in /var/log/spiffy
# Select which "csi" program to use; defaults to the one in $PATH
CSI=csi
# Use a longer trace buffer and a larger nursery size (optional)
CSI_ARGS="-:s1m -:a200"
DATE_FORMAT="+%Y-%m-%d %H:%M:%S"
CONFIG_FILE="/usr/local/libexec/spiffy.scm"
LOG_DIR="/var/log/spiffy"
start() {
if [ ! -d "$LOG_DIR" ]; then
echo "Log directory \"$LOG_DIR\" does not exist."
exit 1
fi
if [ ! -r "$CONFIG_FILE" ]; then
echo "Cannot find or read the configuration file. Please create or check the permissions of the following file: \"$CONFIG_FILE\"."
exit 1
fi
echo "Starting spiffy..."
echo "start: " `date "$DATE_FORMAT"` >> /var/log/spiffy/start-stop.log
(cd /var/log/spiffy; ulimit -c unlimited ; $CSI $CSI_ARGS -s /usr/local/libexec/spiffy.scm &) </dev/null &> /var/log/spiffy/init.log
}
stop() {
echo "Stopping spiffy..."
echo "stop: " `date "$DATE_FORMAT"` >> /var/log/spiffy/start-stop.log
pids=`ps ax | grep "[/]usr/local/libexec/spiffy.scm" | awk '{print $1}'`
if [ -z "$pids" ] ; then
echo "spiffy is not running"
else
for pid in $pids; do
echo "killing " $pid
kill $pid
done
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 1
start
;;
*) exit 1
esac
The spiffy.service file, to be placed in /usr/lib/systemd/system/
[Unit] Description=Spiffy the webserver [Service] Type=oneshot ExecStart=/usr/lib/systemd/scripts/spiffy start ExecStop=/usr/lib/systemd/scripts/spiffy stop RemainAfterExit=yes [Install] WantedBy=multi-user.target
Now you can start, stop or restart Spiffy using the following commands:
$ systemctl start spiffy
$ systemctl stop spiffy
$ systemctl restart spiffy
To enable Spiffy to be started at boot:
$ systemctl enable spiffy