I just figured out how to write a init.d script to create a daemon which runs a php script, and now found I learned the old way. Wonderful...
That being said, my initial experience with systemctl is good, so upward and onward!
Below is what I came up with (with a lot of help) for the old way.
According to /etc/init.d/README
Quote:
Note that traditional init scripts continue to function on a systemd
system. An init script /etc/rc.d/init.d/foobar is implicitly mapped
into a service unit foobar.service during system initialization.
|
So, as a quick fix, I added my old init.d to my new server, rebooted, and then executed systemctl enable soaptest (/etc/init.d/soaptest), and it was added the old way.
For a starting point, can I backward-engineer the equivalent systemctl script? I tried using
systemctl restart soaptest, but no luck. Will doing so not work?
Code:
#!/bin/sh
### BEGIN INIT INFO
# Provides: Test SOAP Simulator
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start SOAP Simulator
# Description: Initiate a given PHP file every 5 seconds to simulate a SOAP server.
### END INIT INFO
SCRIPT="/usr/bin/php /var/www/soap/test_soap.php"
RUNAS=Michael
PIDFILE=/var/run/test_soap.pid
LOGFILE=/var/log/test_soap.log
PROG="SOAP Server"
start() {
if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
echo "$PROG already running" >&2
return 1
fi
echo "Starting $PROG…" >&2
#redirct I/O so non-root user doesn't need special write position to log file.
local CMD="$SCRIPT >&3 2>&1 & echo \$!"
su -c "$CMD" $RUNAS 3>"$LOGFILE" >"$PIDFILE"
echo "$PROG started" >&2
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo "$PROG not running" >&2
return 1
fi
echo "Stopping $PROG…" >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo "$PROG stopped" >&2
}
status() {
if [ -f "$PIDFILE" ] && ps -p $(cat $PIDFILE) >/dev/null;
#if [ -d /proc/$(<$PIDFILE) ];
then
echo "$PROG is running"
else
echo "$PROG is not running"
fi
RETVAL=$?
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
esac