Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run jar file as Daemon on Linux Ubuntu

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 update-rc.d to defaults.

#!/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>> /dev/null >> /dev/null &
            echo $! > $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>> /dev/null >> /dev/null &
            echo $! > $DIR/pid
            echo "${NAME} started ..."
        else
            echo "${NAME} is not running ..."
        fi
    ;;
esac

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 top there is no process with the pid listed.

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 
like image 839
Henok Avatar asked Dec 20 '25 01:12

Henok


1 Answers

I have found another solution for my problem. I use upstart (works only with Ubuntu) to run my jar-File as a daemon. Upstart manages the PIDs. Just add myservice.conf to /etc/init (not /etc/inid.d) and the daemon will be started on boot and you can mangage it as a service. You do not have to make the file runnable or anything else

You can manage the service as normal for example

service myservice restart
service myservice status
...

My Config-File:

description "myservice"
author "your name"

start on runlevel [3]
stop on shutdown

expect fork

script   
    cd /home/username/
    sudo -u username java -jar /home/username/myservice/myservice.jar >/home/username/myservice.log 2>&1
    emit myservice_running
end script

This solution is really easy and works well on my Ubuntu 12.04 Server.

like image 96
Henok Avatar answered Dec 22 '25 18:12

Henok