Sunday, April 30, 2017

Create a service for executable in Ubuntu 16.04 systemd



I am using a web server that I found in here as my executable, many thanks for the developer who developed this web server.

First of all we need to create a script which is capable of starting and stopping the executable and manage a PID file as we start and stop the executable, because systemd will need a PID file to find the main process id of the program.

Parameters


1. so my executable name is http_examples
2. my program is currently locate in /home/altair/Desktop/server/Simple-Web-Server/build
3. and I need to run this program as user altair
4. Start/Stop Script name server_startscript.sh

I will use above four parameters in this tutorial and you need to change them according to your environment, I will highlight them using red color so you can identify what places you need to edit



Creating the start/stop script

1
2
cd /home/altair/Desktop/server/Simple-Web-Server/build
vi server_startscript.sh

Paste the following, make sure to replace the BINARYNAME , press "I" before you paste the code because in VIM you need to press I to insert text to the file or use any other text editor like nano
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#!/bin/sh

COMMANDLINE_PARAMETERS="${2}" #add any command line parameters you want to pass here
D1=$(readlink -f "$0")
BINARYPATH="$(dirname "${D1}")"
cd "${BINARYPATH}"
LIBRARYPATH="$(pwd)"
BINARYNAME="http_examples"


case "$1" in
 start)
  if [ -e server.pid ]; then
   if ( kill -0 $(cat server.pid) 2> /dev/null ); then
    echo "The server is already running, try restart or stop"
    exit 1
   else
    echo "server.pid found, but no server running. Possibly your previously started server crashed"
    echo "Please view the logfile for details."
    rm server.pid
   fi
  fi
  if [ "${UID}" = "0" ]; then
   echo WARNING ! For security reasons we advise: DO NOT RUN THE SERVER AS ROOT
   c=1
   while [ "$c" -le 10 ]; do
    echo -n "!"
    sleep 1
    c=$(($c+1))
   done
   echo "!"
  fi
  echo "Starting the  server"
  if [ -e "$BINARYNAME" ]; then
   if [ ! -x "$BINARYNAME" ]; then
    echo "${BINARYNAME} is not executable, trying to set it"
    chmod u+x "${BINARYNAME}"
   fi
   if [ -x "$BINARYNAME" ]; then
    export LD_LIBRARY_PATH="${LIBRARYPATH}:${LD_LIBRARY_PATH}"     
    "./${BINARYNAME}" ${COMMANDLINE_PARAMETERS} > /dev/null &
     PID=`pidof ${BINARYNAME}`
    ps -p ${PID} > /dev/null 2>&1
    if [ "$?" -ne "0" ]; then
     echo "  server could not start"
    else
     echo $PID > server.pid
    fi
   else
    echo "${BINARNAME} is not exectuable, cannot start   server"
   fi
  else
   echo "Could not find binary, aborting"
   exit 5
  fi
 ;;
 stop)
  if [ -e server.pid ]; then
   echo -n "Stopping the   server"
   if ( kill -TERM $(cat server.pid) 2> /dev/null ); then
    c=1
    while [ "$c" -le 300 ]; do
     if ( kill -0 $(cat server.pid) 2> /dev/null ); then
      echo -n "."
      sleep 1
     else
      break
     fi
     c=$(($c+1)) 
    done
   fi
   if ( kill -0 $(cat server.pid) 2> /dev/null ); then
    echo "Server is not shutting down cleanly - killing"
    kill -KILL $(cat server.pid)
   else
    echo "done"
   fi
   rm server.pid
  else
   echo "No server running (server.pid is missing)"
   exit 7
  fi
 ;;
 restart)
  $0 stop && $0 start ${COMMANDLINE_PARAMETERS} || exit 1
 ;;
 status)
  if [ -e server.pid ]; then
   if ( kill -0 $(cat server.pid) 2> /dev/null ); then
    echo "Server is running"
   else
    echo "Server seems to have died"
   fi
  else
   echo "No server running (server.pid is missing)"
  fi
 ;;
 *)
  echo "Usage: ${0} {start|stop|restart|status}"
  exit 2
esac
exit 0

Save the file. Try to run and stop the executable by using the script.
1
2
chmod +x server_startscript.sh
./server_startscript.sh start

you will see a file called server.pid is created inside the directory. lets stop the executable.
1
./server_startscript.sh stop

Now the server.pid file should be removed from the files.

Creating the systemd service


1
sudo vi /lib/systemd/system/http_examples.service

add the following content and make sure to replace the highlighted areas
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
[Unit]
Description=http_examples
After=network.target
 
[Service]
WorkingDirectory=/home/altair/Desktop/server/Simple-Web-Server/build/
User=altair
Group=altair
Type=forking
ExecStart=/home/altair/Desktop/server/Simple-Web-Server/build/server_startscript.sh start
ExecStop=/home/altair/Desktop/server/Simple-Web-Server/build/server_startscript.sh stop
PIDFile=/home/altair/Desktop/server/Simple-Web-Server/build/server.pid
RestartSec=15
Restart=always
 
[Install]
WantedBy=multi-user.target

Once done, save and close the editor then enable the server and startup script with this command
1
2
3
sudo systemctl --system daemon-reload
sudo systemctl enable http_examples.service
sudo service http_examples start

That's it comment if you got any problems

Angular cli 4 JQuery \ Other Plugins(Underscore js) setup

For this blog I will show you how to setup JQuery and Underscore js with using NPM and without using NPM Lets create a new Angular cli pro...