Linux Daemon not forking? - linux

I am having issues getting my homemade daemon working. This is on Ubuntu
Whenever I call
service mydaemon start
The daemon starts, but never I never get control back to the console. It is like the process is not forking or something....
Here is what I do at the beginning of my daemon process.
pid_t pid;
//fork duplicates the current process
pid = fork();
//The parent process should get a non-zero pid from fork
//The child process should get 0
if (pid < 0) //negative indicates error
{
cout << "Error starting Daemon.";
exit(EXIT_FAILURE);
}
else if (pid > 0) //parent process, exit success
{
cout << "Error starting Daemon. PID=" << pid;
exit(EXIT_SUCCESS);
}
//file permisions??
umask(0);
//Child process set to new session so it is process group leader
pid_t sid = setsid();
if(sid < 0)
{
exit(EXIT_FAILURE);
}
if(chdir("/") < 0)
{
exit(EXIT_FAILURE);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
Here is my inti.d daemon config file.
#! /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: Foo Bar <foobar#baz.org>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.
# 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="my daemon"
NAME=mydaemon
DAEMON=/usr/sbin/$NAME
DAEMON_ARGS=""
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
for i in 0 1 2 3 4 5
do
if [ "$(pidof mysqld)" ]
then
sleep 2
break
else
sleep 2
fi
done
# 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 $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS \
|| 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 --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
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 --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
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 --quiet --pidfile $PIDFILE --name $NAME
return 0
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
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 $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && 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 $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
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: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:

This works for me:
start)
echo "Starting $NAME"
start-stop-daemon --start -m --quiet -b --pidfile $PIDFILE --exec $DAEMON >> $LOG || exit 1
;;
stop)
echo "Stopping $NAME"
start-stop-daemon --stop --pidfile $PIDFILE || exit 1
;;;
Can't get logging to work with the above, but that's related to start-stop-daemon

Related

Bash doesn't interpet \x20 as a space in directory path

I am trying to convert a systemd service to sysvinit, while trying to run the following service named mullvad-vpn;
#!/bin/sh
.....
. /lib/lsb/init-functions
prog=mullvad-daemon
PIDFILE=/var/run/$prog.pid
DESC="Mullvad VPN daemon"
start() {
log_daemon_msg "Starting $DESC" "$prog"
start_daemon -p $PIDFILE /opt/Mullvad\x20VPN/resources/mullvad-daemon -v --disable-stdout-timestamps
if [ $? -ne 0 ]; then
log_end_msg 1
exit 1
fi
if [ $? -eq 0 ]; then
log_end_msg 0
fi
exit 0
}
stop() {
log_daemon_msg "Stopping $DESC" "$prog"
killproc -p $PIDFILE /opt/Mullvad\x20VPN/resources/mullvad-daemon
if [ $? -ne 0 ]; then
log_end_msg 1
exit 1
fi
if [ $? -eq 0 ]; then
log_end_msg 0
fi
.....
Giving me the following result;
sudo service mullvad-daemon.sh start
Starting Mullvad VPN daemon: mullvad-daemon/sbin/start-stop-daemon: unable to stat /opt/Mullvadx20VPN/resources/mullvad-daemon (No such file or directory)
I tried;
double quotes around path with just a space,
single quotes path with just a space, and with backslashes,
double backward backslashes, to try and escape further. All with combinations of \x20 or \xa0.
It works if I remove the space from the folder name and adjust directory, and while it's usable it obviously fails to load some assets. This is probably very simple and the only thing stopping it from being fully functional.
Thank you #user1934428 and #GordonDavisson for putting me on the right track. The solution was to wrap $exec in quotes on line 58 in /lib/lsb/init-functions;
--chdir "$PWD" --exec "$exec" --oknodo --pidfile "$pidfile" -- "$#"
my /etc/init.d/mullvad-daemon.sh now looks like this
#!/bin/sh
### BEGIN INIT INFO
# Provides: mullvad-daemon
# Required-Start: $syslog $local_fs
# Required-Stop: $syslog $local_fs
# Should-Start: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Mullvad VPN daemon
### END INIT INFO
. /lib/lsb/init-functions
prog=mullvad-daemon
PIDFILE=/var/run/$prog.pid
DESC="Mullvad VPN daemon"
start() {
log_daemon_msg "Starting $DESC" "$prog"
start_daemon -p $PIDFILE "/opt/Mullvad VPN/resources/mullvad-daemon" -v --disable-stdout-timestamps
if [ $? -ne 0 ]; then
log_end_msg 1
exit 1
fi
if [ $? -eq 0 ]; then
log_end_msg 0
fi
exit 0
}
stop() {
log_daemon_msg "Stopping $DESC" "$prog"
killproc -p $PIDFILE "/opt/Mullvad VPN/resources/mullvad-daemon"
if [ $? -ne 0 ]; then
log_end_msg 1
exit 1
fi
if [ $? -eq 0 ]; then
log_end_msg 0
fi
}
force_reload() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
force-reload)
force_reload
;;
restart)
stop
start
;;
*)
echo "$Usage: $prog {start|stop|force-reload|restart}"
exit 2
esac

Nginx won't start. /usr/sbin/nginx and /var/run/nginx.pid don't exist

I try to Nginx with /etc/init.d/nginx start. It exits with value 0 and otherwise does absolutely nothing because /usr/sbin/nginx doesn't exist.
Here is /etc/init.d/nginx:
#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx defaults if available
if [ -r /etc/default/nginx ]; then
. /etc/default/nginx
fi
test -x $DAEMON || exit 0
set -e
. /lib/lsb/init-functions
# We made the awk expression more precise:
# https://code.google.com/p/phusion-passenger/issues/detail?id=1060
PID=$(awk -F'[ \t;]+' '/[^#]\<pid/ {print $2}' /etc/nginx/nginx.conf)
if [ -z "$PID" ]
then
PID=/var/run/nginx.pid
fi
# Check if the ULIMIT is set in /etc/default/nginx
if [ -n "$ULIMIT" ]; then
# Set the ulimits
ulimit $ULIMIT
fi
test_nginx_config() {
$DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
retvar=$?
if [ $retvar -ne 0 ]
then
exit $retvar
fi
}
start() {
start-stop-daemon --start --quiet --pidfile $PID \
--retry 5 --exec $DAEMON --oknodo -- $DAEMON_OPTS
}
stop() {
start-stop-daemon --stop --quiet --pidfile $PID \
--retry 5 --oknodo --exec $DAEMON
}
case "$1" in
start)
test_nginx_config
log_daemon_msg "Starting $DESC" "$NAME"
start
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop
log_end_msg $?
;;
restart|force-reload)
test_nginx_config
log_daemon_msg "Restarting $DESC" "$NAME"
stop
sleep 1
start
log_end_msg $?
;;
reload)
test_nginx_config
log_daemon_msg "Reloading $DESC configuration" "$NAME"
start-stop-daemon --stop --signal HUP --quiet --pidfile $PID \
--oknodo --exec $DAEMON
log_end_msg $?
;;
configtest|testconfig)
log_daemon_msg "Testing $DESC configuration"
if test_nginx_config; then
log_daemon_msg "$NAME"
else
exit $?
fi
log_end_msg $?
;;
status)
status_of_proc -p $PID "$DAEMON" nginx
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
exit 1
;;
esac
exit 0
I see that it exits with value 0 because of the test -x $DAEMON || exit 0 line. If I comment that line out then the script will stop suddenly inside of the test_nginx_config() function when it goes to run the daemon (/usr/sbin/nginx) which doesn't exist. There is no /usr/local/nginx/ directory either, for what it's worth.
I'm a total newbie at this stuff so there might be something dumb simple going on but I just need to get this started up. Any help would be greatly appreciated.
Well, if this is your start up script and nginx used to work with it then, Nginx has been removed by something or someone.

Run a mono application at startup without creating a service

I have a mono C# mono application with a simple menu that doesn't even need an input to work. What I want to do is to start the application on startup without using StartUp for ubuntu.I'm using raspbian and want to start the application as a background process, not service.
What I have so far is the command to start it:
start-stop-daemon --start --quiet --pidfile /home/pi/pid/EDM.pid --exec /home/pi/Mono/EDM.exe --test
What I need is to run this command when the OS boots. I've tried this script:
#! /bin/sh
### BEGIN INIT INFO
# Provides: mn
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
# Author:
#
# 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="Mono Daemon"
NAME=EDM.exe
DAEMON=/home/po/Mono/EDM.exe
PIDFILE=/home/pi/pid/EDM.pid
SCRIPTNAME=/etc/init.d/mn.sh
CHUID=pi
# Exit if the package is not installed
[ -x "/home/pi/Mono/EDM.exe" ] || exit 0
# Read configuration variable file if it is present
#[ -r /etc/default/EDM.exe ] && . /etc/default/EDM.exe
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /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/EDM.pid --exec /home/pi/Mono/EDM.exe --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --chuid root --pidfile /home/pi/pid/EDM.pid --exec /home/pi/Mono/EDM.exe -- \
/home/pi/Mono/EDM.exe_ARGS \
|| return 2
}
#
# 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 --quiet --retry=TERM/30/KILL/5 --pidfile /home/pi/pid/EDM.pid --name EDM.exe
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 --quiet --oknodo --retry=0/30/KILL/5 --exec /home/pi/Mono/EDM.exe
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f /home/pi/pid/EDM.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 --quiet --pidfile /home/pi/pid/EDM.pid --name EDM.exe
return 0
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "EDM.exe"
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 $DESC" "EDM.exe"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
#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 $DESC" "EDM.exe"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "EDM.exe"
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: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
:
I use update-rc.d mn defaults and it shows the following errors:
update-rc.d:
using dependency based boot sequencing
insserv: Script mn is broken: incomplete LSB comment.
insserv: missing `Provides:' entry: please add.
insserv: missing `Required-Start:' entry: please add even if empty.
insserv: missing `Required-Stop:' entry: please add even if empty.
insserv: missing `Default-Start:' entry: please add even if empty.
insserv: missing `Default-Stop:' entry: please add even if empty.
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `mn'
insserv: Default-Stop undefined, assuming empty stop runlevel(s) for script `mn'
Any ideas how to do it?
In the end the best way to do this was to make an extremely simple bash script that runs the command:
#!/bin/bash
start-stop-daemon --start --background -m --oknodo --pidfile /home/pi/pid/MonoDaemon.pid --exec /usr/bin/mono -- /home/pi/Mono/EDM.exe
After that I added a row in /etc/init.d/rc.local
/bin/bash /etc/init.d/mnw

init script issue: unexpected then, expecting }

I have an init script (im running Debian) to start serviio a DLNA server. When i go to start the service i get the following error. Any help would be appreciated. Please note: I'm VERY new to linux/bash/debian.
I get this error: Invalid Syntax: unexpected then on line 43, expected }
With this code:
#! /bin/sh
#
# /etc/init.d/serviio
#
#
### BEGIN INIT INFO
# Provides: serviio
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the serviio DLNA server in headless mode
### END INIT INFO
SERVIIO_HOME="/opt/serviio"
SERVIIO_DAEMON="serviio.sh"
SERVIIO_BIN="$SERVIIO_HOME/bin/$SERVIIO_DAEMON"
SERVIIO_USER="serviio"
# Source function library.
. /lib/lsb/init-functions
RETVAL=0
check() {
# Check that we're a privileged user
[ $(id -u) = 0 ] || exit 4
# Check if SERVIIO_HOME exists
test -d "$SERVIIO_HOME" || exit 5
# Check if SERVIIO_BIN is executable
test -x "$SERVIIO_BIN" || exit 5
}
start() {
check
echo -n "Starting Serviio DLNA server: "
/usr/bin/sudo -u $SERVIIO_USER -H $SERVIIO_BIN -headless &
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
touch /var/lock/serviio
log_end_msg 0
else
log_end_msg 1
fi
echo
return $RETVAL
}
stop() {
check
echo -n "Shutting down Serviio DLNA daemon: "
killproc "$SERVIIO_BIN"
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f /var/lock/serviio
echo
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
force-reload)
restart
;;
restart)
restart
;;
condrestart)
if [ -f /var/lock/serviio ]; then
restart
fi
;;
status)
status serviio.sh
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|force-reload}"
RETVAL=2
esac
exit $RETVAL
I can run this script without getting syntax errors,, but I don't have serviio installed. Anyway, my guess would be that $RETVAL is somehow empty, and that it needs to be in quotes:
if [ "$RETVAL" -eq 0 ]; then
Although I would think that would complain about '[' expecting a unary operator.
The next thing to try would be to comment out sections at a time by prepending "#" to the lines in the section until you can narrow down where the problem is. For example, comment out the whole "start" section, or comment out the "check" function (and make sure you comment out the calls to it, too). That should help narrow it down.

Linux waiting for another Daemon to start

I am creating a daemon process. This process depends on MYSQL, however my process alwasy starts at boot before mysql. How do I make my daemon start after mysql starts at boot time?
I do not see any mysql related scripts in the rc.x.d folders such that I can change the numbering.
Do I have put delays in my daemon waiting for mysql to load?
Also, how do I register the daemon such that I can just do
start mydaemon
and the process will start? I just get
"start: unknown job: mydaemon"
Is there any way to report back from the daemon whether or not startup was successful when using the "start" command?
I am using Ubuntu.
Here is my script for startup, based off Ubuntu's provided Skeleton script.
I ran the following to register the daemon already. I know it is running # startup from message log.
update-rc.d mydaemon default
#! /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: Foo Bar <foobar#baz.org>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.
# 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="Daemon"
NAME=mydaemon
DAEMON=/usr/sbin/$NAME
DAEMON_ARGS=""
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /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 $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS \
|| 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 --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
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 --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
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 --quiet --pidfile $PIDFILE --name $NAME
return 0
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
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 $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && 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 $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
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: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:
The SysV init system in modern Ubuntu is legacy support, the main startup system is Upstart, which allows you to specify a dependency on mysql.
start and stop are controls for daemons managed by the upstart system - the control/configuration files for these daemons go to /etc/init

Resources