Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Startup script with systemd in Linux

Can I do This start up service below, there are no errors showing once run, but the server script below does not run!

ln /lib/systemd/aquarium.service aquarium.service
systemctl daemon-reload
systemctl enable aquarium.service
systemctl start aquarium.service

thanks

aquarium.service:

[Unit]
Description=Start aquarium server

[Service]
WorkingDirectory=/home/root/python/code/aquarium/
ExecStart=/bin/bash server.* start
KillMode=process

[Install]
WantedBy=multi-user.target

here is the server.sh script

#!/bin/bash

PID=""

function get_pid {
   PID=`pidof python ./udpthread.py`
}

function stop {
   get_pid
   if [ -z $PID ]; then
      echo "server is not running."
      exit 1
   else
      echo -n "Stopping server.."
      kill -9 $PID
      sleep 1
      echo ".. Done."
   fi
}


function start {
   get_pid
   if [ -z $PID ]; then
      echo  "Starting server.."
      ./udpthread.py &
      get_pid
      echo "Done. PID=$PID"
   else
      echo "server is already running, PID=$PID"
   fi
}

function restart {
   echo  "Restarting server.."
   get_pid
   if [ -z $PID ]; then
      start
   else
      stop
      sleep 5
      start
   fi
}


function status {
   get_pid
   if [ -z  $PID ]; then
      echo "Server is not running."
      exit 1
   else
      echo "Server is running, PID=$PID"
   fi
}

case "$1" in
   start)
      start
   ;;
   stop)
      stop
   ;;
   restart)
      restart
   ;;
   status)
      status
   ;;
   *)
      echo "Usage: $0 {start|stop|restart|status}"
esac
like image 312
Ossama Avatar asked Feb 28 '13 21:02

Ossama


People also ask

How do I start systemd in Linux?

To start a systemd service, executing instructions in the service's unit file, use the start command. If you are running as a non-root user, you will have to use sudo since this will affect the state of the operating system: sudo systemctl start application .

How do I enable systemd service on startup?

2 Enabling and disabling services with systemctlsystemctl is the systemd command for controlling how services start on a Linux system. A service can be enabled, disabled, or masked, and it can be configured to start at boot, on-demand, manually, or prevented from starting under any circumstances.


1 Answers

Try using "Type=forking" and use complete filename.

[Unit]
Description=Start aquarium server

[Service]
WorkingDirectory=/home/root/python/code/aquarium/
Type=forking
ExecStart=/bin/bash server.sh start
KillMode=process

[Install]
WantedBy=multi-user.target

if it not work, post output of this command:

# journalctl -u aquarium.service
like image 112
Victor Aurélio Avatar answered Oct 30 '22 20:10

Victor Aurélio