Handling daemons in the init scripts
by gustav3000 from LinuxQuestions.org on (#5P20N)
Hi! I'm trying to write an init script running lxd-agent daemon. This daemon doesn't have options like -d or --daemon. What's the correct way to handle this daemon? Should I background this program manually in the init bash script using &? What about the PID file? In which package is start-stop-daemon on Slackware? Do I need to build it from SBo?
Here's my /etc/rc.d/rc.lxd-agent script:
Code:#!/bin/sh
PRGNAM="lxd-agent"
BIN="/run/lxd_config/drive/lxd-agent"
PID="/run/lxd-agent.pid"
REQUIRED_DIRS="/run/lxd_config/drive"
lxd_agent_start() {
if [ -s ${PID} ]; then
echo "${PRGNAM} is already running: $(cat ${PIS})"
exit 1
fi
if [ -x ${BIN} ]; then
echo "Starting ${PRGNAM}: ${BIN}"
mkdir -p ${REQUIRED_DIRS}
cd ${REQUIRED_DIRS} && ${BIN} &
echo $! > ${PID}
fi
}
lxd_agent_stop() {
if [ -s ${PID} ]; then
echo "Stopping ${PRGNAM}..."
kill -QUIT $(cat ${PID})
else
echo "${PRGNAM} is already stopped. Exiting..."
exit 1
fi
}
lxd_agent_restart() {
lxd_agent_stop
sleep 5
lxd_agent_start
}
lxd_agent_status() {
if [ -s ${PID} ]; then
echo "${PRGNAM} is running: $(cat ${PID})"
else
echo "${PRGNAM} does not seem to be running"
fi
}
case $1 in
"start")
lxd_agent_start
;;
"stop")
lxd_agent_stop
;;
"restart")
lxd_agent_restart
;;
"status")
lxd_agent_status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
;;
esac
Here's my /etc/rc.d/rc.lxd-agent script:
Code:#!/bin/sh
PRGNAM="lxd-agent"
BIN="/run/lxd_config/drive/lxd-agent"
PID="/run/lxd-agent.pid"
REQUIRED_DIRS="/run/lxd_config/drive"
lxd_agent_start() {
if [ -s ${PID} ]; then
echo "${PRGNAM} is already running: $(cat ${PIS})"
exit 1
fi
if [ -x ${BIN} ]; then
echo "Starting ${PRGNAM}: ${BIN}"
mkdir -p ${REQUIRED_DIRS}
cd ${REQUIRED_DIRS} && ${BIN} &
echo $! > ${PID}
fi
}
lxd_agent_stop() {
if [ -s ${PID} ]; then
echo "Stopping ${PRGNAM}..."
kill -QUIT $(cat ${PID})
else
echo "${PRGNAM} is already stopped. Exiting..."
exit 1
fi
}
lxd_agent_restart() {
lxd_agent_stop
sleep 5
lxd_agent_start
}
lxd_agent_status() {
if [ -s ${PID} ]; then
echo "${PRGNAM} is running: $(cat ${PID})"
else
echo "${PRGNAM} does not seem to be running"
fi
}
case $1 in
"start")
lxd_agent_start
;;
"stop")
lxd_agent_stop
;;
"restart")
lxd_agent_restart
;;
"status")
lxd_agent_status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
;;
esac