18

I have installed no-ip dynamic ddns following this guide: https://www.noip.com/support/knowledgebase/installing-the-linux-dynamic-update-client/

I get the service running by

sudo /usr/local/bin/noip2

However I want the service to start on boot, I tried adding the following script to /etc/init.d/noip2.sh

#######################################################
#! /bin/sh
# . /etc/rc.d/init.d/functions  # uncomment/modify for your killproc
case "$1" in
    start)
    echo "Starting noip2."
    /usr/local/bin/noip2
    ;;
    stop)
    echo -n "Shutting down noip2."
    killproc -TERM /usr/local/bin/noip2
    ;;
    *)
    echo "Usage: $0 {start|stop}"
    exit 1
esac
exit 0
#######################################################

Followed by:

sudo chmod +x /etc/init.d/noip2.sh
sudo update-rc.d noip2.sh defaults

Now I should be able to start the service with

sudo service noip2 start

But i'm not. When I run journalctl -xe I get the following:

-- Unit noip2.service has begun starting up.
Nov 03 12:36:11 media systemd[3111]: noip2.service: Failed to execute command: Exec format error
Nov 03 12:36:11 media systemd[3111]: noip2.service: Failed at step EXEC spawning /etc/init.d/noip2.sh: Exec format error
-- Subject: Process /etc/init.d/noip2.sh could not be executed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- The process /etc/init.d/noip2.sh could not be executed and failed.
--
-- The error number returned by this process is 8.
Nov 03 12:36:11 media systemd[1]: noip2.service: Control process exited, code=exited status=203
Nov 03 12:36:11 media systemd[1]: noip2.service: Failed with result 'exit-code'.
Nov 03 12:36:11 media systemd[1]: Failed to start noip2.service.
-- Subject: Unit noip2.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support

Updated info for user PerDuck: I get the following errors trying your solution... :( Tried adding

RestartSec=30

At least it keeps trying now but it still do not start. I can still start it with sudo /usr/local/bin/noip2

The errors:

Nov 03 23:26:42 media systemd[1]: noip2.service: Service hold-off time over, scheduling restart.
Nov 03 23:26:42 media systemd[1]: noip2.service: Scheduled restart job, restart counter is at 5.
Nov 03 23:26:42 media systemd[1]: Stopped noip2 service.
Nov 03 23:26:42 media systemd[1]: noip2.service: Start request repeated too quickly.
Nov 03 23:26:42 media systemd[1]: noip2.service: Failed with result 'start-limit-hit'.
Nov 03 23:26:42 media systemd[1]: Failed to start noip2 service.
0

1 Answer 1

29

Since Ubuntu 15.04 the standard way to control background processes (and much, much more) is systemd. I suggest switching from your init.d script to a systemd unit:

Create the file /etc/systemd/system/noip2.service with the following content (and drop your init.d scripts):

[Unit]
Description=noip2 service

[Service]
Type=forking
ExecStart=/usr/local/bin/noip2
Restart=always

[Install]
WantedBy=default.target

Then issue

sudo systemctl daemon-reload

to make systemd aware of the new unit (systemd caches unit files and this command makes systemd reconsider its cache).

Now you can try to start and stop your unit and see its status:

sudo systemctl status noip2
sudo systemctl start  noip2
sudo systemctl status noip2
sudo systemctl stop   noip2
sudo systemctl status noip2

To have the unit started at boot time you need to enable it:

sudo systemctl enable noip2

To disable autostart at boot time you must disable the unit:

sudo systemctl disable noip2

Most of the time five commands are sufficient to control a units behaviour:

systemctl start   $unit   # starts a unit NOW
systemctl stop    $unit   # stops a unit NOW
systemctl status  $unit   # shows status
systemctl enable  $unit   # starts a unit at boot time (but not NOW)
systemctl disable $unit   # stops autostart (but doesn't stop the unit NOW)

You may also enable autostart and start the unit imediately or disable autostart and stop it at once:

systemctl enable  --now $unit   # enable and start in one go
systemctl disable --now $unit   # disable and stop in one go

Update

Some research revealed the noip2 runs as a daemon, i.e. when you start it, it creates another process that runs in background (so called forking) and the foreground process immediately returns (exits). That's why the init.d script and the systemd unit failed: They started noip2 just to see it immediately exits. Hence, systemd tried to restart it over and over to no avail. (By default, systemd restarts a process at most 5 times within 10 seconds or so before giving up and leaving it in failed state.)

To tell systemd the unit is of type forking add the line

Type=forking

to the [Service] section as I just did in the snippet above. This tells systemd to expect the main process to return immediately but instead watch the process spawned (forked) by noip2.

6
  • Thank you for your reply. I tried your solution but got some errors when trying to start the service. You can see the errors shown in sudo systemctl status noip2 at the bottom of my post. Nov 3, 2018 at 22:32
  • @jesper I'm on my mobile now, so cannot answer properly. noip2 forks. Try adding Type=forking to the [Service] section. See gist.github.com/NathanGiesbrecht/…
    – PerlDuck
    Nov 4, 2018 at 10:24
  • Thanks, that seems to fix it! I will wait some more and do some more testing before approving your answer! :D Nov 4, 2018 at 11:23
  • @Jesper.Lindberg Very good. I just updated the answer to reflect the "forking" type (I'm back on my PC :-)).
    – PerlDuck
    Nov 4, 2018 at 11:24
  • @Jesper.Lindberg I just noticed someone else already supplied an answer to a question similar to yours: askubuntu.com/q/903411/504066. Never mind.
    – PerlDuck
    Nov 4, 2018 at 11:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.