0

I have problems with the nut (network UPS tools) package. So I started digging in and making some experiments.

How can I create a fake service that is just called with a stop parameter at some runlevel as it was in a good old init.d epoch?

As I see many packages still rely on that but I can't get the desired behavior.

For example, I have a simple script file /etc/init.d/halt and a link to it /etc/rc0.d/K07halt. It's supposed to be called in the end of the halt runlevel.

But if I do service halt stop or /etc/rc0.f/K07halt I get the error:

Stopping K07halt (via systemctl): K07halt.serviceFailed to stop
K07halt.service: Unit K07halt.service not loaded.
failed!

How can I make such a dummy script to be executed even if there is no such service/daemon?

Change something in the header? Declare some flags for /lib/lsb/init-functions? How to fool the Systemd and make it to call the script unconditionally?

Please don't tell me not to use init.d because I am not writing a new daemon but trying to fix something that already exists.

  • Why you must fooling systemd? It's not needed. systemd supported legacy init scripts too. Just place it in /etc/init.d and do systemctl [enable|disable|start|stop] your script. – Yurij Goncharuk Apr 8 '18 at 23:04
  • Nice tip, my dear friend! But as I said in the question I've got "service not loaded" error message that I am really surprised about. And that actually is the question - how to make it work as an old good init.d, because the legacy seems broken for now. – Sap Apr 9 '18 at 14:10
  • Legacy is works. Please see my answer. – Yurij Goncharuk Apr 9 '18 at 16:18
0

I wrote LSB service for Ubuntu 16.04 and don't have any problems:

# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.4 LTS
Release:    16.04
Codename:   xenial

Adding new service fake-service in /etc/init.d with 755 permissions:

#!/bin/sh
# fake-service   Bring up/down fake-service
#
### BEGIN INIT INFO
# Provides:     fake-service
# Default-Start:    2 3 4 5
# Default-Stop:
#Required-Start:
#Required-Stop:
# Should-Start:
# Should-Start:
# Short-Description: Bring up/down fake-service
# Description: Bring up/down fake service
### END INIT INFO

DAEMON=
NAME=fake-service
DESC="fake-service"
PIDFILE=/var/run/fake-service.pid
SCRIPTNAME=/etc/init.d/fake-service

. /lib/lsb/init-functions

case "$1" in
start)
    log_daemon_msg "Starting fake-service ..." "$NAME"
    touch $PIDFILE
    log_end_msg $?
;;
stop)
    log_daemon_msg "Stoping fake-service ..." "$NAME"
    rm -f $PIDFILE
    log_end_msg $?
;;
status)
    status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
;;
*)
    log_action_msg "Usage: $0 {start|stop|status}"
    exit 2
    ;;
esac

exit 0

Adding it to the run-levels:

# update-rc.d fake-service defaults

From man 8 update-rc.d:

When run with the defaults option, update-rc.d makes links named /etc/rcrunlevel.d/[SK]NNname that point to the script /etc/init.d/name, using runlevel and dependency information from the init.d script LSB comment header.

Testing service:

Starting:

# service fake-service start
# service fake-service status
● fake-service.service - LSB: Bring up/down fake-service
   Loaded: loaded (/etc/init.d/fake-service; bad; vendor preset: enabled)
   Active: active (exited) since Mon 2018-04-09 19:14:18 MSK; 4s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3643 ExecStop=/etc/init.d/fake-service stop (code=exited, status=0/SUCCESS)
  Process: 3713 ExecStart=/etc/init.d/fake-service start (code=exited, status=0/SUCCESS)

Apr 09 19:14:18 ubuntu16-04 systemd[1]: Starting LSB: Bring up/down fake-service...
Apr 09 19:14:18 ubuntu16-04 fake-service[3713]:  * Starting fake-service ... fake-service
Apr 09 19:14:18 ubuntu16-04 fake-service[3713]:    ...done.
Apr 09 19:14:18 ubuntu16-04 systemd[1]: Started LSB: Bring up/down fake-service.

Stoping:

# service fake-service stop
# service fake-service status
● fake-service.service - LSB: Bring up/down fake-service
   Loaded: loaded (/etc/init.d/fake-service; bad; vendor preset: enabled)
   Active: inactive (dead) since Mon 2018-04-09 19:15:03 MSK; 3s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3757 ExecStop=/etc/init.d/fake-service stop (code=exited, status=0/SUCCESS)
  Process: 3713 ExecStart=/etc/init.d/fake-service start (code=exited, status=0/SUCCESS)

Apr 09 19:14:18 ubuntu16-04 systemd[1]: Starting LSB: Bring up/down fake-service...
Apr 09 19:14:18 ubuntu16-04 fake-service[3713]:  * Starting fake-service ... fake-service
Apr 09 19:14:18 ubuntu16-04 fake-service[3713]:    ...done.
Apr 09 19:14:18 ubuntu16-04 systemd[1]: Started LSB: Bring up/down fake-service.
Apr 09 19:15:03 ubuntu16-04 systemd[1]: Stopping LSB: Bring up/down fake-service...
Apr 09 19:15:03 ubuntu16-04 fake-service[3757]:  * Stoping fake-service ... fake-service
Apr 09 19:15:03 ubuntu16-04 fake-service[3757]:    ...done.
Apr 09 19:15:03 ubuntu16-04 systemd[1]: Stopped LSB: Bring up/down fake-service.
  • Thank you. But the test you made is more about real service. And in my case there is just a script that works at system shutdown. There is no any PID file and it is not started at any level. It must be only called with "stop" argument. I see some legacy software that rely on such behavior. And "nut" (Network UPS Tools) is one of them. Can you please make the same test but without PID file creation and without calling the script with "start" option? May be something is wrong with my system only? – Sap Apr 9 '18 at 23:50
  • Sorry I was busy, so return to the question just now. I tested it as you want. Service just printing me Stopped LSB: Bring up/down fake-service.. So any instructions is not executed in stop rule. I tried trick with adding to status block of case just exit 0 but it's not helped. It's impossible to fool initial system (neither systemd nor initd). When service is being started then it is being registered with initial system. When you call stop, initial system see own register and do (or not) real actions. – Yurij Goncharuk Apr 10 '18 at 19:27
  • Why you don't want to create stop script as ordinary service with trivial start (touch pid)? – Yurij Goncharuk Apr 10 '18 at 19:27
  • The main reason - I am not the author. I am trying to fix something. And I am trying to avoid any noticeable changes. Also I see at least three software packages that are broken because of this reason and no one cares. Some shutdown scripts just are not executed. I am still not sure that the problem exists. May I am just doing something wrong. Still don't believe that such a big incompatibility passed unnoticed in Ubuntu 16.04. – Sap Apr 10 '18 at 21:22
  • Can you describe more details about problems with nut package? Maybe I can help you? – Yurij Goncharuk Apr 10 '18 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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