There's more than a few questions on this already, but I've hit an issue with a method I've used in the past. On older versions of Ubuntu, I used this tutorial.
I created a file
/etc/init.d/myservice.sh
The file contains the following:
#!/bin/bash
SERVICE_NAME=myservice
PATH_TO_JAR=/usr/local/MyProject/MyProject.jar
PID_PATH_NAME=/var/run/$SERVICE_NAME.pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stopping ...";
kill $PID;
echo "$SERVICE_NAME stopped ...";
rm $PID_PATH_NAME
echo "$SERVICE_NAME starting ..."
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac
I made sure that the file had the correct permissions/owner
I run
sudo service myservice start
, and I get the following error.Failed to start myservice.service: Unit myservice.service not found.
When I looked this up, I found a lot of conflicting information. I found stackoverflow answers saying that you couldn't use this method anymore, and others saying that this method is still supported. What is the correct method, assuming I'm using Ubuntu 16.04 (and maybe point me to a tutorial?), and if this method is still valid, can someone point me in the direction of why it wouldn't be working now?