3

I am developing an automatic watering system on my Pi. The code is C#, run under Mono.

I want the system to start automatically when the Pi boots up, so I am trying to create a script in init.d.

I copied a script for something else, and just changed the program to run, but I keep getting

 WateringSystem.serviceFailed to start WateringSystem.service: Unit WateringSystem.service failed to load: No such file or directory

Here is my code:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          WateringSystem
# Required-Start:    $local_fs $network $time $syslog
# Required-Stop:     $local_fs $network $time $syslog
# Should-Start:      ntp
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start WateringSystem daemon at boot time
# Description:       Start WateringSystem daemon at boot time.
### END INIT INFO
# pidfiles:          $prefix/var/WateringSystem.pid
################################################################################


# add to the shared library search path
#export LD_LIBRARY_PATH=/lib:/usr/local/lib:/usr/lib
NAME=WateringSystem
RUN_DIRECTORY=/home/pi/$NAME
INSTALL_DIR=/home/pi/$NAME
DAEMON_PID=/var/run/$NAME.pid
DAEMON="mono -debug $INSTALL_DIR/WateringSystem.exe"
LOGDIR=/var/log

. /lib/lsb/init-functions

do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started
        start-stop-daemon --start --quiet --background --make-pidfile --pidfile $DAEMON_PID --exec $DAEMON --test > /dev/null \
                || return 1
        if [ "$1" -eq 1 ] ; then
            start-stop-daemon --start --quiet --no-close --background --make-pidfile --pidfile $DAEMON_PID --exec $DAEMON --chdir $RUN_DIRECTORY \
                $DAEMON_ARGS \
                || return 2
        else
            start-stop-daemon --start --quiet --background --make-pidfile --pidfile $DAEMON_PID --exec $DAEMON --chdir $RUN_DIRECTORY -- \
                $DAEMON_ARGS -L$LOGDIR/$NAME\
                || return 2
        fi
}


case "$1" in
    start)
        echo "Listing"
        ls -ld $DAEMON
        log_daemon_msg "Starting $DESC" "$NAME"
        do_start 0
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
    start-trace)
        log_daemon_msg "Starting $DESC" "$NAME"
        do_start 1
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
    stop)
        log_daemon_msg "Stopping $DESC" "$NAME"
        start-stop-daemon --stop --oknodo --pidfile $DAEMON_PID --name $NAME\
            --exec $DAEMON --retry 5
        RETVAL="$?"
        [ "$RETVAL" = 2 ] && return 2
        rm -f $DAEMON_PID
        exit "$RETVAL"
        ;;
    restart)
        $0 stop  && sleep 2
        $0 start
        ;;
    status)
        start-stop-daemon --status --pidfile $DAEMON_PID
        RUNNING=$?
        if [ $RUNNING -eq 0 ]; then
            echo -n "Running ($RUNNING) PID:"
            cat $DAEMON_PID
        else
            echo "NOT Running ($RUNNING)"
        fi
        status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?

        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
esac

exit 0

I have never used start-stop-daemon before, so I don't know what is wrong.

Do I have to add some more files somewhere to tell systemd how to run my server?

2

init.d may still work - systemd tries to make a .service from init.d scripts, but you would be better to write a systemd service.

Services should all specify full paths to executables.

You appear to have a more fundamental problem. $INSTALL_DIR/WateringSystem.exe does not look like an ARM executable.

The first step before automating any script is to run it manually and ensure it actually works.

  • The command I wanted to run is /usr/bin/mono -debug $INSTALL_DIR/WateringSystem.exe. Now I have fixed the obvious mistake you pointed out, and put the full path in, it works! Thanks! – Nikki Locke Mar 2 '16 at 9:51
  • It is starting the program now, but the stop command isn't killing the process. How can I debug that? – Nikki Locke Mar 2 '16 at 13:07

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.