17

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
jww
  • 97,681
  • 90
  • 411
  • 885
Ossama
  • 2,401
  • 7
  • 46
  • 83

1 Answers1

29

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
Victor Aurélio
  • 2,355
  • 2
  • 24
  • 48
  • Thanks for that, however I get this when using the exact script you typed up there. know that /bin/bash server.sh start does start the script!! beaglebone:~# systemctl status aquarium.service aquarium.service - Start aquarium server Loaded: loaded (/etc/systemd/system/aquarium.service; enabled) Active: failed (Result: exit-code) since Fri, 01 Mar 2013 18:18:26 +1100; 23s ago Process: 12545 ExecStart=/bin/bash server.sh start (code=exited, status=200/CHDIR) Main PID: 12482 (code=exited, status=200/CHDIR) CGroup: name=systemd:/system/aquarium.service – Ossama Mar 01 '13 at 07:20
  • 1
    I forgot to add this Mar 01 18:18:26 beaglebone (bash)[12545]: Failed at step CHDIR spawning /bin/bash: No such file or directory – Ossama Mar 01 '13 at 07:21
  • hmm, what system you are using ? On ArchLinux this files are symbolic links: `% ls -l /bin/sh /bin/bash lrwxrwxrwx 1 root root 15 Jan 26 21:19 /bin/bash -> ../usr/bin/bash* lrwxrwxrwx 1 root root 15 Jan 26 21:19 /bin/sh -> ../usr/bin/bash*` Try replacing "/bin/bash" with "/bin/sh", and make sure that files exists using "ls -l" – Victor Aurélio Mar 01 '13 at 14:01
  • Hi, i am using angstrom on the beaglebone /bin/sh does not work too!! This was supposed to be easy to set up – Ossama Mar 02 '13 at 00:26
  • 4
    Try, "ExecStart=/bin/sh /home/root/python/code/aquarium/server.sh start" – Victor Aurélio Mar 02 '13 at 14:30
  • Thank you. The problem was acctually in the path, i should have put /Python instead of /python cap P. but this was not a problem when i executed the script from shell using. Thanks all – Ossama Mar 03 '13 at 09:12
  • Thank you! I couldn't figure out how to activate python virtual environment when using systemd – Winand Mar 24 '22 at 14:42