start-stop-daemon

Hello all,

Does anyone have a sample init.d script that contains the start, stop, restart commands that the sample xojo code also looks for? There are many samples on the web, but I’ve been unsuccessful in getting the pair I have tried to work without what I think is “forcing” the app to start. Forcing is remarking code in the project

#If TargetLinux Or TargetARM Then // Daemonize 
  #If Not DebugBuild Then // Do not try to daemonize a debug build
    System.Log( System.LogLevelCritical, "Trying to daemonize the Axcys application.")
    
    If (args(1) = "start" Or args(1) = "-d" ) Then // Check for command-line parameter to daemonize  <<<< ===  remark here to force
       If Not App.Daemonize Then
        System.Log( System.LogLevelCritical, "Could not daemonize the Axcys application.")
        Return -1
      End If
   End If       <<<< ===  remark here to force
    
    System.Log( System.LogLevelCritical, "Daemonized the Axcys application.")
  #EndIf
#EndIf

This is the init.d code I have been working with - note the comment that ‘Forces’ it to work, but really doesn’t work, in sync with xojo unless remarked as shown above…

#!/bin/bash
# AxcysFacilitySecurityManager daemon
# chkconfig: 345 20 80
# description: AxcysFacilitySecurityManager daemon
# processname: myapp

# 	/home/pi/Public/axcys/AxcysFacilitySecurityManager/AxcysFacilitySecurityManager &
#	/home/pi/Public/axcys/gate/AxcysEmbeddedInterface &

# DAEMON_PATH="/home/wes/Development/projects/myapp"
# DAEMON=myapp
# DAEMONOPTS="-my opts"
# NAME=myapp
# DESC="My daemon description"
# DAEMONOPTS="-d"

# AxcysFacilitySecurityManager
# DAEMON_PATH="/home/pi/Public/axcys/AxcysFacilitySecurityManager/AxcysFacilitySecurityManager"
DAEMON_PATH="/home/pi/Public/axcys/AxcysFacilitySecurityManager/"
DAEMON=AxcysFacilitySecurityManager
DAEMONOPTS=" &"

NAME=AxcysFacilitySecurityManager
DESC="Axcys Startup"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
'start')
	printf "%-50s" "Starting $NAME..."
	cd $DAEMON_PATH
	PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
	#echo "Saving PID" $PID " to " $PIDFILE
        if [ -z $PID ]; then
            printf "%s\
" "Fail"
        else
            echo $PID > $PIDFILE
            /home/pi/Public/axcys/AxcysFacilitySecurityManager/AxcysFacilitySecurityManager &      <<<< ===  Use to force
            printf "%s\
" "Ok"
        fi
;;
'status')
        printf "%-50s" "Checking $NAME..."
        if [ -f $PIDFILE ]; then
            PID=`cat $PIDFILE`
            if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
                printf "%s\
" "Process dead but pidfile exists"
            else
                echo "Running"
            fi
        else
            printf "%s\
" "Service not running"
        fi
;;
'stop')
        printf "%-50s" "Stopping $NAME"
            PID=`cat $PIDFILE`
            cd $DAEMON_PATH
        if [ -f $PIDFILE ]; then
            kill -HUP $PID
            printf "%s\
" "Ok"
            rm -f $PIDFILE
        else
            printf "%s\
" "pidfile not found"
        fi
;;

'restart')
  	$0 stop
  	$0 start
;;

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

I would really appreciate any feedback/info/help on this! My goal is to use this to both auto start that app, but also to programmatically restart the app too.

Thanks all!
Tim

This is an error I see in one of the log files. Note that it is looking for a ‘.service’ file…

Again, any help would be greatly appreciated!

Tim

Do you run with appropriate superuser privileges and runlevels?

I found the following note in my HowTo collection (should work for Raspi as well):

[quote]How to add a program to run at startup in Ubuntu Linux Server edition

Problem: Add a program to run at startup in Ubuntu Linux Server via the command line.

First of all, you’re going to need root access.

1. Find the location of the program you wish to run at bootup.
For example, MyProgram is located in

/home/me/programs/MyProgram

2. We need to create a script file in the init.d folder and we need superuser privileges for that.

2a. Run the command

sudo touch /etc/init.d/RunMyProgram

It’ll prompt you for the root password, enter it. Make sure the file has been created in that directory.

2b. Make the file executable.

sudo chmod +x /etc/init.d/RunMyprogram

3. Open that file in a text editor
(nano /etc/init.d/RunMyProgram)

4. We’re going to create a simple script that will simply call our program.
Type this bit of code in that text file. Replace the second line with the file path for your program. That is all that should be in the text file.

#!/bin/bash /home/me/programs/MyProgram &

4a. This script will automatically run as root.
If you want your program to run as a standard unprivileged user, put in a 'su ’ before the second line.

5. We need to assign our script to a certain runlevel
(at what state in Linux’s bootup you wish to run the program).

5a. Simply run

sudo update-rc.d /etc/init.d/RunMyProgram defaults

and that will put you at the appropriate runlevels.
[/quote]

Well first of all there’s nothing that the “Xojo code looks for”.

  1. Make sure you’re App class is subclassed from ServiceApplication.
  2. Include the source function library . /etc/rc.d/init.d/functions
  3. In the start section, use the daemon command thusly:
daemon /path/to/application
  1. Always daemonize the app in the open event
  2. Use killproc <processname> to stop
  3. Use chkconfig to tell the system when your app is supposed to be running.
    Ugh. I hate our forum sometimes… that’s supposed to be 4, 5 & 6

Consider using start-stop-daemon to launch your app as a daemon. If you do, start-stop-daemon should be used to stop it. start-stop-daemon also makes it easy to run your app as a user that is less privileged than root.

How to LSBize an Init Script has information on how to format your bash script. update-rc.d has information on how to set the run levels.

Here is a simple example. It assumes the name of the script is AxcysFacilitySecurityManager and the name of the user is axcysfacilitysecuritymanager. The script should be in /etc/init.d and the run levels should be updated with sudo update-rc.d AxcysFacilitySecurityManager defaults.

[code]#!/bin/sh

BEGIN INIT INFO

Provides: AxcysFacilitySecurityManager

Required-Start: $local_fs $syslog $time $network

Required-Stop: $local_fs $syslog $time $network

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: Axcys Facility Security Manager

Description: Axcys Facility Security Manager

END INIT INFO

case $1 in
start)
start-stop-daemon --quiet --chuid axcysfacilitysecuritymanager --start --exec /home/pi/Public/axcys/AxcysFacilitySecurityManager/AxcysFacilitySecurityManager
;;
stop)
start-stop-daemon --quiet --chuid axcysfacilitysecuritymanager --stop --exec /home/pi/Public/axcys/AxcysFacilitySecurityManager/AxcysFacilitySecurityManager
;;
restart|force-reload)
$0 stop && $0 start
;;
status)
ps u -U axcysfacilitysecuritymanager
;;
*)
echo “Usage: sudo service AxcysFacilitySecurityManager {start|stop|restart|force-reload|status}”
;;
esac[/code]

Hope that helps.

Thank you all!
Got it to work starting the app.

Much appreciated!

Tim

I have several different executables to start and run/control. Is it best to have a different script for each? If not, then how to control each one separately?

Thank you Frederick for the sample! I appreciate your work and the comments suggestions by the others!

Tim

Yes. Each app should have its own script

Thank you Greg,
Tim