1

I have a task to design a service out of a bash script in order to be called in the way service ... start/stop/restart.

The script that is to become a service is a infinite while loop which wakes up every minute and does some checking. I call it like this:

start() {
echo -n $"Starting $DESC:"
DAEMON_ARGS=$(xmlstarlet sel -T -t -m "/config/input/sensor/device/resource" -v "concat(../../@type, ' ', ../../@dev, ' ', @res)" -n $CONFIGURATION_FILE | extract_devices)        # get device names out of XML file
echo "daemon args $DAEMON_ARGS"
start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS
echo}

pid and daemon are defined like this:

PIDFILE="/var/run/detection.pid"
NAME="jblub_control_loop.sh"
DAEMON="/root/test_det/${NAME}"

When I run

./detection start (I also tried to copy into init.d and to run with service detection start)

The init script simply doesn't get out of the loop and stays blocked in the start-stop-daemon function. When I comment out infinite while loop inside my jblub_control_loop.sh it passes but no pid file is created.

My question is how to properly start script with an infinite loop as a service and why my pid file is not created.

1

For a start,

use the --background switch so it is forked. use the -m switch to create a PID file

start-stop-daemon --start --background -m --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS

For a copmplete answer, see https://stackoverflow.com/questions/16139940/what-is-start-stop-daemon-in-linux-scripting

Enjoy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.