Embedded Linux: Start an executable as a service using daemon

Embedded linux usually has code running in the application space apart from Kernel space. As an embedded developer, we may have some executable code/binaries developed in C, C++, python or java. The main purpose is these software binaries could be to run motors, read values from sensors, capture images, process information or perform machine learning. If you are wondering on how to make your executable run as a process in the background without you having to turn it on in the OS, then you need to create something called as a daemon process.

Let me demonstrate my daemon script on a linux device. I have used raspberry pi in this case.

I have tested the same on my Ubuntu 24.04 computer as well.

The first thing that you do is to navigate to the directory /etc/init.d

$ cd /etc/init.d        

Second, copy this script to this location.

$ sudo nano StartUpAsAService.sh        

The content of the shell script is as follows:

#!/bin/sh

### BEGIN INIT INFO
# Provides: myservice
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Put a short description of the service here
# Description: Put a long description of the service here
### END INIT INFO

# Change the next 3 lines to suit where you install your script and what you want to call it
DIR=/home/raja/Desktop
DAEMON=$DIR/StartMotor.py 
# Replace this with the name of your program. Here, I’ve #a C executable whose #permissions I have set to be executable. 
# chmod 755 StartMotor
DAEMON_NAME=MyMotor # Replace this with the name of your service

# Add any command line options for your daemon here
DAEMON_OPTS=””

# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO #from Python.
DAEMON_USER=root

# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_NAME.pid

. /lib/lsb/init-functions

do_start () {
log_daemon_msg “Starting the program for Python serial test $DAEMON_NAME daemon”
start-stop-daemon –start –background –pidfile $PIDFILE –make-pidfile –user $DAEMON_USER –chuid $DAEMON_USER –startas $DAEMON — $DAEMON_OPTS
log_end_msg $?
}
do_stop () {
log_daemon_msg “Stopping the program for Python serial test $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        

After copying this script and editing it, you have to do a one time set up.

Link the script to the OS.

$ sudo update-rc.d StartUpAsAService.sh defaults        

Check it if it has linked properly:

$ ls -l /etc/rc?.d/*StartUpAsAService.sh        

The final step: Start the service.

$ sudo /etc/init.d/StartUpAsAService.sh start        

The other capabilities of this script are as follows -

$ /etc/init.d/myservice.sh status        

That’s it folks! Your program boots up even after a reboot of your linux computer.

You can pivot this script to start up services of your choice like the JVM’s of an application server, etc too.

To view or add a comment, sign in

More articles by Gurajapu Raja Sumant

Insights from the community

Others also viewed

Explore topics