Start bash script as service daemon
Imagine you need to execute a custom service upon system startup and have it continue running. This guide will show you how to run a custom script at system boot and how to run it as a service.
Chances are, most of you are already familiar with the command
sudo service service-name start
Let's begin here how you may execute it. i am taking example of an " ng serve" command to run the angular project.
So, basically to run an angular project you go inside the project directory and type this command to run the project. Just imagine, when your system start and you do not need to do all those manual tasks, like open command line > change the folder directory > then run the above command and start your coding.
This is really a bit boring, doing same job each time, so why not write a script which perform same job for me at system startup.My Project folder structure for Example:
~/projects/AngualerApp
So, my AngularApp is under the user's Directory > projects. And When i open the terminal, I need to do following.
cd ~/projects/AngularApp
ng serve
// Output after command
Node server listening on localhost:4200
So, first i will write a sell script inside some where in system as:
suppose my path for this file is
~/projects/custome_service.sh
#!/usr/bin/env bash
// Change to the working folder
cd ~/projects/AngularApp
// Run the command
ng serve >/dev/null 2>&1
// write the IO to /dev/null
And saved it as custome_service.sh and also add +x permission to the file, to make it executable.
sudo chmod +x ~/projects/custome_service.sh
Now moving forward, need to take a look at
Systemd service unit
First, you need to create a systemd startup script as: angular-app.service , you may give your own name. which will have the following code snippet.
#This file should be placed at this location:
# /etc/systemd/system/
[Unit]
Description=Angular service
[Service]
ExecStart=~/projects/custome_service.sh
# This path should match your script path, from where you are going to run the command
[Install]
WantedBy=multi-user.target
- Description: The service about
- ExecStart: Field provides a full path of the actual script to be executed.
- WantedBy: The line WantedBy=multi-user.target in a service is essentially the same as specifying "this service should start in runlevels 3, 4 and 5" in SysVinit systems: it tells systemd that this service should be started as part of normal system start-up, whether or not a local GUI is active.
For more options to add in .service file visit the manual of the service:
$ man systemd.service
Now time to enable the written service file to get identified by systemd:
# chmod 664 /etc/systemd/system/angular-app.service
# systemctl daemon-reload
# systemctl enable angular-app.service
If you wish to test your script before you reboot run:
systemctl start angular-app.service
Now, how you are going to run it as service angular-server start and running it as daemon
I am going to write a file under /etc/init.d/ as "angular-server", which will do the work of daemon
#!/bin/bash
################################
##
## This file should be placed under:
##
## /etc/init.d/
##
## With name: angular-server
##
################################
. /lib/lsb/init-functions
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
DAEMON=~/projects/custome_service.sh
NAME=angular-serve
DESC=angular-serve
PIDFILE=/var/run/${NAME}.pid
USER=ubuntu
# you may change the user name as the system user name.
export LOGNAME=$USER
test -x $DAEMON || exit 0
set -e
function _start() {
start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $USER:$USER --background --make-pidfile --exec $DAEMON --
}
function _stop() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE --oknodo --retry 3
rm -f $PIDFILE
}
function _status() {
start-stop-daemon --status --quiet --pidfile $PIDFILE
return $?
}
case "$1" in
start)
echo -n "Starting $DESC: "
_start
echo "ok"
;;
stop)
echo -n "Stopping $DESC: "
_stop
echo "ok"
;;
restart|force-reload)
echo -n "Restarting $DESC: "
_stop
sleep 1
_start
echo "ok"
;;
status)
echo -n "Status of $DESC: "
_status && echo "running" || echo "stopped"
;;
*)esac
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
exit 1
;;
exit 0
So, my script is ready. This script is divided in two section as Declaration and Definition.
After saving the file, change the permission to make it executable.
When ever you tends to stop your custom startup service, you have options to stop it, restart it.
Hope this helpful !!!
Run Bash Script as Daemon or Start as Service: Comprehensive Guide