I have to write a portal init.d script for a program that runs on foreground (no daemon option; no pidfile option) and logs to stdout/stderr.
The program should log into syslog service. The program should run as non root user, e.g. nobody.
The init.d file should run unter Debian and RHEL bases system like SLES 11.
I do not have the ability to install additional programs, like RHEL based procs on Debian systems.
Here are my problems
daemonize
does not exists on SLESstartproc
has no background optionstart-stop-daemon
has no background option and no user option.
How to route the logs syslog? Should I use >& /dev/log ? or 2>&1 | logger -t prog
A lot of examples that I found on the internet depends on /etc/init.d/functions
(does not exists on SUSE), /etc/rc.status
or /etc/rc.d/init.d/functions
Here is my current approch, that seems only work for SUSE:
#!/bin/bash
#
# node_exporter This script starts and stops the node_exporter daemon
#
# chkconfig: - 85 15
# description: Node Exporter is a Prometheus exporter for hardware and OS metrics
BEGIN INIT INFO
Provides: node_exporter
Required-Start: $local_fs $network
Required-Stop: $local_fs $network
Default-Start: 2 3 4 5
Default-Stop: 0 1 6
Short-Description: start and stop node_exporter
Description: Node Exporter is a Prometheus exporter for hardware and OS metrics
END INIT INFO
NAME=node_exporter
DAEMON=/usr/local/bin/$NAME
DAEMON_ARGS="--web.listen-address=:9100"
PIDFILE=/var/run/$NAME.pid
LOGFILE=/var/log/$NAME.log
. /lib/lsb/init-functions
start() {
echo -n "Starting $NAME: "
start_daemon $DAEMON $DAEMON_ARGS &
echo $! > $PIDFILE
echo "done."
}
stop() {
echo -n "Stopping $NAME: "
killproc -p $PIDFILE $DAEMON
echo "done."
}
status() {
if pidofproc -p $PIDFILE $DAEMON; then
echo "$NAME not running."
else
echo "$NAME running."
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
;;
esac
exit 0
Question: Is &
safe to use for init.d scripts? What about LSB default? How I use them? I think, follow LSB should be cross linux distribution safe.
Since I need functions like auto-restart, should I use /etc/inittab instead a traditional init.d script?