HerpaMoTeH
Posts: 26
Joined: Tue Apr 17, 2012 7:14 am

Writing a daemon script

Thu Dec 05, 2013 10:25 am

Hi,
So I have the following problem: I wrote down a daemon script based on the skeleton example in the /etc/init.d directory, but whenever I try to start the script I get a no such file or directory error.
Here's the script which I wrote :

Code: Select all

#! /bin/sh
### BEGIN INIT INFO
# Provides:          skeleton
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

# Author: Ivaylo Ivanov <example@test>

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="MonoDaemon"
NAME=MonoDaemon
DAEMON=/usr/bin/mono
DAEMON_ARGS=/home/pi/Mono/Vyshka.exe
PIDFILE=/home/pi/pid/MonoDaemon.pid
SCRIPTNAME=/etc/init.d/MonoDaemon

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
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 --pidfile /home/pi/pid/MonoDaemon.pid --exec /usr/bin/mono --test > /dev/null || return 1
	start-stop-daemon --start --background -m --oknodo --pidfile /home/pi/pid/MonoDaemon.pid --exec /usr/bin/mono -- /home/pi/Mono/Vyshka.exe || return 2
	# Add code here, if necessary, that waits for the process to be ready
	# to handle requests from services started subsequently which depend
	# on this one.  As a last resort, sleep for some time.
}

#
# Function that stops the daemon/service
#
do_stop(){
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
	start-stop-daemon --stop --retry=TERM/30/KILL/5 --pidfile /home/pi/pid/MonoDaemon.pid --name MonoDaemon
	RETVAL="$?"
	[ "${RETVAL}" = 2 ] && return 2
	# Wait for children to finish too if this is a daemon that forks
	# and if the daemon is only ever run from this initscript.
	# If the above conditions are not satisfied then add some other code
	# that waits for the process to drop all resources that could be
	# needed by services started subsequently.  A last resort is to
	# sleep for some time.
	start-stop-daemon --stop --oknodo --retry=0/30/KILL/5 --exec /usr/bin/mono
	[ "$?" = 2 ] && return 2
	# Many daemons don't delete their pidfiles when they exit.
	rm -f /home/pi/pid/MonoDaemon.pid
	return "${RETVAL}"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	#
	# If the daemon can reload its configuration without
	# restarting (for example, when it is sent a SIGHUP),
	# then implement that here.
	#
	start-stop-daemon --stop --signal 1 --pidfile /home/pi/pid/MonoDaemon.pid --name MonoDaemon
	return 0
}

case "$1" in
  start)
	[ "${VERBOSE}" != no ] && log_daemon_msg "Starting MonoDaemon" "MonoDaemon"
	do_start
	case "$?" in
		0|1) [ "${VERBOSE}" != no ] && log_end_msg 0 ;;
		2) [ "${VERBOSE}" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "${VERBOSE}" != no ] && log_daemon_msg "Stopping MonoDaemon" "MonoDaemon"
	do_stop
	case "$?" in
		0|1) [ "${VERBOSE}" != no ] && log_end_msg 0 ;;
		2) [ "${VERBOSE}" != no ] && log_end_msg 1 ;;
	esac
	;;
  status)
       status_of_proc "/usr/bin/mono" "MonoDaemon" && exit 0 || exit $?
       ;;
  #reload|force-reload)
	#
	# If do_reload() is not implemented then leave this commented out
	# and leave 'force-reload' as an alias for 'restart'.
	#
	#log_daemon_msg "Reloading MonoDaemon" "MonoDaemon"
	#do_reload
	#log_end_msg $?
	#;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting MonoDaemon" "MonoDaemon"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	#echo "Usage: /etc/init.d/MonoDaemon {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: /etc/init.d/MonoDaemon {start|stop|status|restart|force-reload}" >&2
	exit 3
	;;
esac

:
I added the script as a service through the update-rc.d command and when I run service --status-all I see the MonoDaemon service, but when I try to start it I get this error:

Code: Select all

pi@raspberrypi /etc/init.d $ sudo service MonoDaemon start
env: /etc/init.d/MonoDaemon: No such file or directory
Any help is going to be appreciated :)

forenbenutzer
Posts: 148
Joined: Thu Aug 02, 2012 7:08 pm

Re: Writing a daemon script

Thu Dec 05, 2013 1:08 pm

Code: Select all

chmod a+x /etc/init.d/MonoDaemon
did you?
Pitendo - Case And Emulator Project - http://edv-huber.com/index.php/problemloesungen/12-pitendo

HerpaMoTeH
Posts: 26
Joined: Tue Apr 17, 2012 7:14 am

Re: Writing a daemon script

Thu Dec 05, 2013 1:46 pm

Same error :(

Code: Select all

pi@raspberrypi /etc/init.d $ sudo chmod a+x MonoDaemon 
pi@raspberrypi /etc/init.d $ sudo service MonoDaemon start
env: /etc/init.d/MonoDaemon: No such file or directory
pi@raspberrypi /etc/init.d $ 

WebPi
Posts: 262
Joined: Wed Apr 10, 2013 6:47 pm
Location: Birmingham, UK
Contact: Website

Re: Writing a daemon script

Thu Dec 05, 2013 4:51 pm

What happens if you start it like this:

Code: Select all

sudo /etc/init.d/MoonoDaemon start
raspberrywebserver.com - Raspberry Pi tutorials
LinuxWebServers.net - Linux Web Server tutorials and examples
pyplate.com - Python web publishing tool

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Writing a daemon script

Thu Dec 05, 2013 4:57 pm

Try running it like this:
sudo sh -x /etc/init.d/MoonoDaemon start

The -x flag should produce more lines of tracing output than you need.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

HerpaMoTeH
Posts: 26
Joined: Tue Apr 17, 2012 7:14 am

Re: Writing a daemon script

Fri Dec 06, 2013 9:13 am

Here's the output after I removed all of the empty lines from the script, because it showed some error on them:

Code: Select all

pi@raspberrypi /etc/init.d $ ls
alsa-utils              hostname.sh            mtab.sh          rsync
apache2                 hwclock.sh             mysql            rsyslog
binfmt-support          ifplugd                networking       sendsigs
bluetooth               kbd                    network-manager  single
bootlogs                keyboard-setup         nfs-common       skeleton
bootmisc.sh             killprocs              ntp              ssh
checkfs.sh              kmod                   pppd-dns         sudo
checkroot-bootclean.sh  lightdm                procps           triggerhappy
checkroot.sh            MonoDaemon             pulseaudio       udev
console-setup           motd                   rc               udev-mtab
cron                    mountall-bootclean.sh  rc.local         umountfs
dbus                    mountall.sh            rcS              umountnfs.sh
dphys-swapfile          mountdevsubfs.sh       README           umountroot
fake-hwclock            mountkernfs.sh         reboot           urandom
halt                    mountnfs-bootclean.sh  rmnologin        vsftpd
hdparm                  mountnfs.sh            rpcbind          x11-common
pi@raspberrypi /etc/init.d $ sudo chmod a+x MonoDaemon 
pi@raspberrypi /etc/init.d $ sudo sh -x /etc/init.d/MonoDaemon start
+ PATH=/sbin:/usr/sbin:/bin:/usr/bin
+ DESC=MonoDaemon
+ NAME=MonoDaemon
+ DAEMON=/usr/bin/mono
+ DAEMON_ARGS=/home/pi/Mono/Vyshka.exe
+ PIDFILE=/home/pi/pid/MonoDaemon.pid
+ SCRIPTNAME=/etc/init.d/MonoDaemon
+ . /lib/init/vars.sh
/etc/init.d/MonoDaemon: 23: .: Can't open /lib/init/vars.sh
pi@raspberrypi /etc/init.d $ cd /lib/init/
pi@raspberrypi /lib/init $ ls
bootclean.sh  mount-functions.sh  swap-functions.sh  tmpfs.sh  vars.sh
pi@raspberrypi /lib/init $ 

WebPi
Posts: 262
Joined: Wed Apr 10, 2013 6:47 pm
Location: Birmingham, UK
Contact: Website

Re: Writing a daemon script

Fri Dec 06, 2013 10:26 am

Did you copy any code off a web page? The errors on empty lines makes me wonder if there are some invisible unicode characters causing problems for bash.
raspberrywebserver.com - Raspberry Pi tutorials
LinuxWebServers.net - Linux Web Server tutorials and examples
pyplate.com - Python web publishing tool

HerpaMoTeH
Posts: 26
Joined: Tue Apr 17, 2012 7:14 am

Re: Writing a daemon script

Fri Dec 06, 2013 11:31 am

No, I copied the code from the skeleton script which is a init.d example script.

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Writing a daemon script

Fri Dec 06, 2013 3:27 pm

HerpaMoTeH wrote:Here's the output after I removed all of the empty lines from the script, because it showed some error on them:

Code: Select all

/etc/init.d/MonoDaemon: 23: .: Can't open /lib/init/vars.sh
Your pi user doesn't have the right permissions for that file.
sudo chmod 755 /lib/init/vars.sh
should fix it.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

HerpaMoTeH
Posts: 26
Joined: Tue Apr 17, 2012 7:14 am

Re: Writing a daemon script

Fri Dec 06, 2013 3:29 pm

DougieLawson wrote:
HerpaMoTeH wrote:Here's the output after I removed all of the empty lines from the script, because it showed some error on them:

Code: Select all

/etc/init.d/MonoDaemon: 23: .: Can't open /lib/init/vars.sh
Your pi user doesn't have the right permissions for that file.
sudo chmod 755 /lib/init/vars.sh
should fix it.
Same error :(
Output:

Code: Select all

pi@raspberrypi /lib/init $ sudo chmod 755 /lib/init/vars.sh
pi@raspberrypi /lib/init $ sudo sh -x /etc/init.d/MonoDaemon start
+ PATH=/sbin:/usr/sbin:/bin:/usr/bin
+ DESC=MonoDaemon
+ NAME=MonoDaemon
+ DAEMON=/usr/bin/mono
+ DAEMON_ARGS=/home/pi/Mono/Vyshka.exe
+ PIDFILE=/home/pi/pid/MonoDaemon.pid
+ SCRIPTNAME=/etc/init.d/MonoDaemon
+ . /lib/init/vars.sh
/etc/init.d/MonoDaemon: 23: .: Can't open /lib/init/vars.sh

benjaoming
Posts: 2
Joined: Fri Jan 29, 2016 3:26 pm

Re: Writing a daemon script

Tue Sep 20, 2016 5:13 pm

[deleted]
Last edited by benjaoming on Thu Sep 22, 2016 9:00 am, edited 1 time in total.

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Writing a daemon script

Tue Sep 20, 2016 5:37 pm

Since the previous post is almost three years old, long before systemd hit Raspbian, I'm pretty sure that's not right.

Return to “Raspberry Pi OS”