Linux Initscript Example to start and stop process during booting

The below shell script uses start-stop-daemon from “/sbin/start-stop-daemon” to execute the program during booting so that the process is started boottime. In the below example, DAEMON=$PWD/helloworld.bin shows that our program is helloworld.bin and is located in current directory from where the script is executed.

$ vim
# Must be a valid filename
#This is the command to be run, give the full pathname

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

case "$1" in
        echo -n "Starting daemon: "$NAME
	start-stop-daemon --start -m --background --quiet --pidfile $PIDFILE --startas $DAEMON -- $DAEMON_OPTS
        echo "."
        echo -n "Stopping daemon: "$NAME
	start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
        echo "."
        echo -n "Restarting daemon: "$NAME
	start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
	start-stop-daemon --start -m --background --quiet --pidfile $PIDFILE --startas $DAEMON -- $DAEMON_OPTS
	echo "."

	echo "Usage: "$1" {start|stop|restart}"
	exit 1

exit 0

The above script can be executed as “bash start” to start the daemon.

Related :   How to disable and / or enable ICMP ping from Linux host ?

If we want this initscript to be executed everytime during booting sequence, then we need to copy this to /etc/init.d and run the update-rc.d command once as below,

$ chmod 777
$ sudo mv /etc/init.d/start_daemon
$ sudo update-rc.d start_daemon defaults
$ sudo reboot


Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )


Leave a Comment

Android Android Applications Android Build system Android Commands Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded