pythonlinuxbashshellstart-stop-daemon

Linux start-stop-daemon directory error calling shell/python script


I am just getting acquainted with Linux and I cannot seem to get the start-stop-daemon to run a python script due to directory issues. In a linux file structure I have the files:

~/test.txt

THIS LINE IS A TEST

~/test.py

#!/usr/bin/python
import time

with open("test.txt") as f:
    while True:
        try:
            print("Hello World")
            print(f.readline())
            time.sleep(2) 
        except KeyboardInterrupt:
            f.close()
            break

~/test.sh

#!/bin/bash

echo "SHELL SCRIPT SUCCESS" > /var/log/test.log
cd ~/
./test.py > /var/log/test.log

Upon calling sudo bash ~/test.sh from any directory the test.log is populated as expected with the stdout originating from test.py. For some reason, starting the following start-stop-daemon service script WILL generate a test.log but does NOT populate it with the stdout:

/etc/init.d/test

#!/bin/sh

### BEGIN INIT INFO
# Provides:     Python test script
# Required-Start:   $remote_fs $syslog
# Required-Stop:    $remote_fs $syslog
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description:    Prints out daemonized argument
# Description:      Creates output of argument
### END INIT INFO

DAEMON_DIR=/home/alex
DAEMON=$DAEMON_DIR/test.sh
DAEMON_NAME=test

DAEMON_OPTS="hello"
DAEMON_USER=root
PYTHON=/usr/bin/python

PIDFILE=/var/run/$DAEMON_NAME.pid

. /lib/lsb/init-functions

do_start () {
    log_daemon_msg "Starting system $DAEMON_NAME daemon"
    #start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --exec $PYTHON --startas $DAEMON 
    start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --chuid $DAEMON_USER --startas /bin/bash  /home/alex/test.sh
    log_end_msg $? 
}

do_stop () {
    log_daemon_msg "Stopping system $DAEMON_NAME daemon"
    start-stop-daemon --stop --pidfile $PIDFILE --retry 10
    log_end_msg $?
}

case "$1" in

    start|stop)
    do_${1}
    ;;

    restart|reload|force-reload)
    do_stop
    do_start
    ;;

    status)
    status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
    ;;

    *)
    echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
    exit 1
    ;;

esac
exit 0

Is this a directory issue that can be addressed within the start-stop-daemon? Alternatively I'd be open to other methods of script servicing that can persist through a cold boot (i.e. no cron jobs)


Solution

  • Try calling cd using an absolute path, for example /home/alexjg/ instead of ~/; the reason it was broken before is that in your example you're using sudo which keeps the home directory of the user running it. However when you're calling the bash script from init it will use root's home directory instead which doesn't contain test.py.

    The file is created because the redirection is still succeeding; however because starting Python failed there was no output.