title: Run jar file as Daemon on Linux Ubuntu
body: <p>I want to install a bot to my Teamspeak3 and run this bot as a daemon on startup. I wrote my own script and copied it to init.d and then added it with <code>update-rc.d</code> to defaults.</p>
  
  <pre><code>#!/bin/sh
  #
  # JTS3ServerBot Script
  #
  USER="ts"
  NAME="jts3"
  DIR="/home/ts/jts3/"
  case $1 in
      start)
          echo "Starting ${NAME} ..."
          if [ ! -f $DIR/pid ]; then
              sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2&gt;&gt; /dev/null &gt;&gt; /dev/null &amp;
              echo $! &gt; $DIR/pid
              echo "${NAME} started ..."
          else
              echo "${NAME} is already running ..."
          fi
      ;;
      stop)
          if [ -f $DIR/pid ]; then
              PID=$(cat $DIR/pid);
              echo "Stopping ${NAME} ..."
              kill $PID;
              echo "${NAME} stopped ..."
              rm $DIR/pid
          else
              echo "${NAME} is not running ..."
          fi
      ;;
      restart)
          if [ -f $DIR/pid ]; then
              PID=$(cat $DIR/pid);
              echo "Stopping ${NAME} ...";
              kill $PID;
              echo "${NAME} stopped ...";
              rm $DIR/pid
  
              echo "Starting ${NAME} ..."
              sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2&gt;&gt; /dev/null &gt;&gt; /dev/null &amp;
              echo $! &gt; $DIR/pid
              echo "${NAME} started ..."
          else
              echo "${NAME} is not running ..."
          fi
      ;;
  esac
  </code></pre>
  
  <p>A pid file in generated, but if i try to kill the process with this pid i get an error that the process does not exist. If i use <code>top</code> there is no process with the pid listed.</p>
  
  <pre><code>root@vps-1023645-8462:~# service jts3 start
  Starting jts3 ...
  jts3 started ...
  root@vps-1023645-8462:~# cat /home/ts/jts3/pid 
  10206
  root@vps-1023645-8462:~# kill 10206
  bash: kill: (10206) - No such process
  
    PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND           
   1762 ts        20   0 1881m  14m 3408 S    0  1.4 215:47.28 ts3server_linux    
  32356 ts        20   0  164m 1576 1336 S    0  0.2   0:09.85 tsdnsserver_lin 
  </code></pre>