2

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.

  1. I created a file /etc/init.d/myservice.sh

  2. 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

  1. I made sure that the file had the correct permissions/owner

  2. 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?

  • 1
    Dude, if you do it on a modern Linux distro there is 99% chance you have systemd. Google creating "systemd startup service". Downvoting this q – MAQ Jul 26 '17 at 22:49
  • 4
    I find it perfectly plausible a non-technical person wont know the init system they have used all along was changed to systemd. – Rui F Ribeiro Jul 26 '17 at 22:57
  • 1
    I found the answer here https://askubuntu.com/a/674844/672379 , please contribute to the reputation of the person who answered – GypsyCosmonaut Jul 26 '17 at 22:58
  • @KWubbufetowicz I'm curious as to why you didn't phrase your question something closer to "One way to do this is by using systemd". I mentioned that I found other methods, I was asking what happened to the init.d method. You could have even answered the question (if you had read it) – trueCamelType Jul 27 '17 at 01:24
  • 2
    @KWubbufetowicz - "Dude", we try to help people here, not berate them for not knowing things, especially when they're clearly making an effort to learn and understand. – cas Jul 27 '17 at 03:04
  • 2
    @cas Sorry guys, I definitely could have been more open-minded here. – MAQ Jul 27 '17 at 13:23

1 Answers1

0

I was able to find an interesting article on why init needed to be replaced, and what it was replaced with. To quote it "A init process starts serially i.e., one task starts only after the last task startup was successful and it was loaded in the memory. This often resulted into delayed and long booting time. However, systemd was not designed for speed but for getting the things done neatly which in turns avoid all the UN-necessary delay."

So, to answer my own question, it looks like systemd replaced init.d for some valid reasons, and other methods, like what is mentioned in the comments by GypsyCosmonaut is to just have a startup script in ~/.config/autostart which also comes with a handy Startup Applications tool if you're using desktop.

init.d example


Just in case anyone still really wants to do what I was originally trying, you just need a link to the /etc/init.d directory.

For example, if you have a .jar file that needs to run using commands similar to /etc/init.d/yourProgram start or /etc/init.d/yourProgram stop, you'd just do something similar to:

sudo ln -s /path/to/yourProgram.jar /etc/init.d/yourProgram

The above method also works if you point it to a script that starts your program instead of a .jar

init.d start on boot


If you want that service to start on startup, use:

sudo service yourProgram defaults

This will automatically look in /etc/init.d/ and find your link to the actual script.

  • 1
    systemd did not replace van Smoorenbug rc+init on Ubuntu. systemd replaced Upstart. Upstart replaced van Smoorenburg rc, which, moreover, had already had startpar for several years before that, making a nonsense of the claim that van Smoorenburg rc was inherently serial. This answer has not been the way to do things for over a decade on Ubuntu, and is based upon a false history. – JdeBP Jul 27 '17 at 15:44