halt-local.service start during startup

Hello.

/etc/init.d/halt-local is call twice : during startup and during shutdown.

During startup, Here is the beginning of journald :

août 26 11:55:28 linux-7fci systemd[1]: Reached target Sockets.
août 26 11:55:28 linux-7fci systemd[1]: Reached target Basic System.
août 26 11:55:28 linux-7fci systemd[1]: Starting Bluetooth service...
août 26 11:55:28 linux-7fci systemd[1]: Starting /etc/init.d/boot.local Compatibility...
août 26 11:55:28 linux-7fci systemd[1]: Starting Avahi mDNS/DNS-SD Stack...
août 26 11:55:28 linux-7fci systemd[1]: Started Self Monitoring and Reporting Technology (SMART) Daemon.
août 26 11:55:28 linux-7fci avahi-daemon[1209]: Found user 'avahi' (UID 483) and group 'avahi' (GID 482).
août 26 11:55:28 linux-7fci systemd[1]: Starting Dynamic Kernel Modules System...
août 26 11:55:28 linux-7fci avahi-daemon[1209]: Successfully dropped root privileges.
août 26 11:55:28 linux-7fci avahi-daemon[1209]: avahi-daemon 0.6.32 starting up.
août 26 11:55:28 linux-7fci systemd[1]: Starting Save/Restore Sound Card State...
août 26 11:55:28 linux-7fci systemd[1]: Starting Machine Check Exception Logging Daemon...
août 26 11:55:28 linux-7fci systemd[1]: **Starting halt.local.service...**
août 26 11:55:28 linux-7fci systemd[1]: Starting Name Service Cache Daemon...
août 26 11:55:28 linux-7fci systemd[1]: Starting VirtualBox Linux kernel module...
.....
.....
**août 26 11:55:28 ASUS-JC-NEW  ETC_INIT-D_HALT-LOCAL[22536]: /backup_sys/000_COMMON/Bin/incron_backup_incron called by /etc/init.d/halt.local - Run @ 2017_08_26__11h55**
.....
.....
août 26 11:55:28 linux-7fci systemd[1]: Network Manager is not active.
août 26 11:55:28 linux-7fci systemd[1]: Dependency failed for Network Manager Wait Online.
août 26 11:55:28 linux-7fci systemd[1]: NetworkManager-wait-online.service: Job NetworkManager-wait-online.service/start failed with result 'dependency'.
août 26 11:55:28 linux-7fci systemd[1]: Started Save/Restore Sound Card State.
août 26 11:55:28 linux-7fci systemd[1]: Started Machine Check Exception Logging Daemon.
août 26 11:55:28 linux-7fci systemd[1]: **Started halt.local.service.**
août 26 11:55:28 linux-7fci systemd[1]: Started Name Service Cache Daemon.
août 26 11:55:28 linux-7fci kernel: vboxdrv: Found 8 processor cores
août 26 11:55:28 linux-7fci kernel: vboxdrv: TSC mode is Invariant, tentative frequency 2394550550 Hz
août 26 11:55:28 linux-7fci kernel: vboxdrv: Successfully loaded version 5.1.26_SUSE (interface 0x002a0000)
août 26 11:55:28 linux-7fci ModemManager[1303]: <info>  ModemManager (version 1.4.14) starting in system bus...
août 26 11:55:28 linux-7fci dbus[1247]: [system] Activating via systemd: service name='org.freedesktop.PolicyKit1' unit='polkit.service'

.....
.....
.....

During shutdown, Here is the end of journald :


.....
.....
.....
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped target Multi-User System.
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopping Self Monitoring and Reporting Technology (SMART) Daemon...
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopping CUPS Printing Service...
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopping Command Scheduler...
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopping Avahi mDNS/DNS-SD Stack...
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopping irqbalance daemon...
août 26 15:48:28 ASUS-JC-NEW systemd[2936]: Stopped target Default.
août 26 15:48:28 ASUS-JC-NEW bluetoothd[1246]: Stopping SDP server
août 26 15:48:28 ASUS-JC-NEW systemd[2936]: Stopped target Basic System.
août 26 15:48:28 ASUS-JC-NEW bluetoothd[1246]: Exit
août 26 15:48:28 ASUS-JC-NEW systemd[2936]: Stopped target Timers.
août 26 15:48:28 ASUS-JC-NEW systemd[2936]: Reached target Shutdown.
août 26 15:48:28 ASUS-JC-NEW systemd[1]: **Stopping halt.local.service**...
août 26 15:48:28 ASUS-JC-NEW systemd[2936]: Starting Exit the Session...
août 26 15:48:28 ASUS-JC-NEW systemd[2936]: Stopped target Sockets.
août 26 15:48:28 ASUS-JC-NEW systemd[2936]: Stopped target Paths.
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopping SuSEfirewall2 phase 2...
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopping Machine Check Exception Logging Daemon...
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopping MiniDLNA UPnP-A/V and DLNA media server...
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopping Modem Manager...
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped /etc/init.d/after.local Compatibility.
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped target Login Prompts.
.....
.....
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopping Save/Restore Sound Card State...
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped Daily Cleanup of Temporary Directories.
**août 26 15:48:28 ASUS-JC-NEW ETC_INIT-D_HALT-LOCAL[22536]: /backup_sys/000_COMMON/Bin/incron_backup_incron called by /etc/init.d/halt.local - Run @ 2017_08_26__15h48**
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped target Sound Card.
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Unmounted /run/user/1000/gvfs.
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped Self Monitoring and Reporting Technology (SMART) Daemon.
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped irqbalance daemon.
.....
.....
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped Modem Manager.
août 26 15:48:28 ASUS-JC-NEW systemd[2936]: Received SIGRTMIN+24 from PID 22489 (kill).
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped RealtimeKit Scheduling Policy Service.
août 26 15:48:28 ASUS-JC-NEW systemd[2998]: Received SIGRTMIN+24 from PID 22466 (kill).
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped Restore /run/initramfs on shutdown.
août 26 15:48:28 ASUS-JC-NEW systemd[1]: **Stopped halt.local.service.**
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped Autostart Headless Virtual Box VMs.
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped Save/Restore Sound Card State.
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped User Manager for UID 481.
août 26 15:48:28 ASUS-JC-NEW systemd[1]: Stopped User Manager for UID 1000.
.....
.....
août 26 15:48:37 ASUS-JC-NEW systemd[1]: Stopped Remount Root and Kernel File Systems.
août 26 15:48:37 ASUS-JC-NEW systemd[1]: Removed slice system-systemd\x2dfsck.slice.
août 26 15:48:37 ASUS-JC-NEW systemd[1]: Reached target Shutdown.
août 26 15:48:37 ASUS-JC-NEW systemd[1]: **Starting /etc/init.d/halt.local Compatibility...**
août 26 15:48:37 ASUS-JC-NEW systemd[1]: **halt-local.service: Main process exited, code=exited, status=127/n/a**
août 26 15:48:37 ASUS-JC-NEW systemd[1]: **Failed to start /etc/init.d/halt.local Compatibility. **(*really print in red*)
août 26 15:48:37 ASUS-JC-NEW systemd[1]: **halt-local.service: Unit entered failed state.**
août 26 15:48:37 ASUS-JC-NEW systemd[1]: **halt-local.service: Failed with result 'exit-code'.**
août 26 15:48:37 ASUS-JC-NEW systemd[1]: Reached target Final Step.
août 26 15:48:37 ASUS-JC-NEW systemd[1]: Starting Power-Off...
août 26 15:48:37 ASUS-JC-NEW systemd[1]: Shutting down.
août 26 15:48:37 ASUS-JC-NEW systemd-shutdown[1]: Sending SIGTERM to remaining processes...
août 26 15:48:37 ASUS-JC-NEW haveged[593]: haveged: Stopping due to signal 15
août 26 15:48:37 ASUS-JC-NEW haveged[593]: haveged starting up
août 26 15:48:37 ASUS-JC-NEW systemd-journald[398]: Journal stopped

here is /usr/lib/systemd/system/halt-local.service file

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=/etc/init.d/halt.local Compatibility
ConditionFileIsExecutable=/etc/init.d/halt.local
DefaultDependencies=no
After=shutdown.target
Before=final.target

[Service]
Type=oneshot
ExecStart=/etc/init.d/halt.local
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes

here is /etc/init.d/halt.local

#! /bin/sh
#
# Copyright (c) 2002 SuSE Linux AG Nuernberg, Germany.  All rights reserved.
#
# Author: Werner Fink, 1998
#         Burchard Steinbild, 1998
#
# /etc/init.d/halt.local
#
# script with local commands to be executed from init on system shutdown
#
# Here you should add things, that should happen directly before shuting
# down.
#

/backup_sys/000_COMMON/Bin/incron_backup_incron


here is /backup_sys/000_COMMON/Bin/incron_backup_incron

#!/bin/bash
#
##################################
#                                            #
#    /backup_sys/000_COMMON/Bin/incron_backup_incron
#                                            #
#    {2017_08_24}                        #
#                                            #
#    ¨version:15-0-a¨                    #
#                                            #
##################################
#
# object
#
########################################################
#
TIMESTAMP=$(date --date now +%Y_%m_%d__%Hh%M)
logger -t _ETC_INIT-D_HALT-LOCAL "/backup_sys/000_COMMON/Bin/incron_backup_incron called by /etc/init.d/halt.local - Run @ $TIMESTAMP"
#

Any help is welcome

Have you by chance enabled halt.local explicitly and unintentionally? (which would result in the /etc/init.d/halt.local SysVinit script to be directly to be started at boot).

Check with

systemctl status halt.local

Other than that, I would only expect the systemd halt-local.service to take care of the necessary before shutdown.

Focussing on this journal output…

août 26 15:48:37 ASUS-JC-NEW systemd[1]: Reached target Shutdown.
août 26 15:48:37 ASUS-JC-NEW systemd[1]: **Starting /etc/init.d/halt.local Compatibility...**
août 26 15:48:37 ASUS-JC-NEW systemd[1]: **halt-local.service: Main process exited, code=exited, status=127/n/a**
août 26 15:48:37 ASUS-JC-NEW systemd[1]: **Failed to start /etc/init.d/halt.local Compatibility. **(*really print in red*)
août 26 15:48:37 ASUS-JC-NEW systemd[1]: **halt-local.service: Unit entered failed state.**
août 26 15:48:37 ASUS-JC-NEW systemd[1]: **halt-local.service: Failed with result 'exit-code'.**

…it appears that this fails due to being called too late to do anything useful. (For example it runs after local filesystems have been unmounted.)

You should consider putting your backup script in the /usr/lib/systemd/system-shutdown/ directory instead.

man systemd-shutdown

DESCRIPTION
systemd-halt.service is a system service that is pulled in by halt.target and is responsible for the actual system halt. Similarly, systemd-poweroff.service is pulled in by
poweroff.target, systemd-reboot.service by reboot.target and systemd-kexec.service by kexec.target to execute the respective actions.

   When these services are run, they ensure that PID 1 is replaced by the /usr/lib/systemd/systemd-shutdown tool which is then responsible for the actual shutdown. Before shutting
   down, this binary will try to unmount all remaining file systems, disable all remaining swap devices, detach all remaining storage devices and kill all remaining processes.
   It is necessary to have this code in a separate binary because otherwise rebooting after an upgrade might be broken — the running PID 1 could still depend on libraries which are
   not available any more, thus keeping the file system busy, which then cannot be re-mounted read-only.
   Immediately before executing the actual system halt/poweroff/reboot/kexec systemd-shutdown will run all executables in /usr/lib/systemd/system-shutdown/ and pass one arguments to
   them: either "halt", "poweroff", "reboot" or "kexec", depending on the chosen action. All executables in this directory are executed in parallel, and execution of the action is
   not continued before all executables finished.
   Note that systemd-halt.service (and the related units) should never be executed directly. Instead, trigger system shutdown with a command such as "systemctl halt" or suchlike.

Actually, from testing I now see systemd-shutdown runs too late as well.

Instead, create a custom service (eg /etc/systemd/system/backup.service) that runs earlier…


[Unit]
Description=backup at shutdown
Before=shutdown.target reboot.target halt.target

[Service]
ExecStart=/bin/true
ExecStop=/path/to/backup-script.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

then enable and start with

systemctl enable backup.service
systemctl start backup.service

Similar approaches discussed here…
https://www.reddit.com/r/archlinux/comments/5v7m27/does_placing_scripts_in/
https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemd

If i remember , yes I have enable explicitly the service by running the command

systemctl enable halt-local.service ;systemctl start halt-local.service ;
ASUS-JC-NEW:~ # systemctl -l status halt.local
● halt.local.service
   Loaded: loaded (/etc/init.d/halt.local; bad; vendor preset: disabled)
   Active: active (exited) since Mon 2017-08-28 10:42:54 CEST; 1h 32min ago
     Docs: man:systemd-sysv-generator(8)
    Tasks: 0 (limit: 512)

Aug 28 10:42:31 linux-7fci halt.local[1214]: '/backup_sys/000_COMMON/SBin_SYS/incron-script' -> '/backup_sys/¬001_INSTALL/¬079_INCRON/001_COMPILE_TMP_DIR/INCRON-0.5.12_2017_08_28__10h42/etc/incron-script'
Aug 28 10:42:31 linux-7fci halt.local[1214]: chown -R user_install:users  /backup_sys/¬001_INSTALL/*
Aug 28 10:42:45 ASUS-JC-NEW halt.local[1214]: chown -R user_install:users  /backup_sys/¬001_INSTALL/.*
Aug 28 10:42:54 ASUS-JC-NEW halt.local[1214]: chown -R user_install:users  /backup_sys/000_COMMON/Bin/*
Aug 28 10:42:54 ASUS-JC-NEW halt.local[1214]: .
Aug 28 10:42:54 ASUS-JC-NEW halt.local[1214]: .
Aug 28 10:42:54 ASUS-JC-NEW halt.local[1214]: .
Aug 28 10:42:54 ASUS-JC-NEW halt.local[1214]: You can close
Aug 28 10:42:54 ASUS-JC-NEW halt.local[1214]: .
Aug 28 10:42:54 ASUS-JC-NEW systemd[1]: Started halt.local.service.

AFAIU, it is supposed to be called by the systemd halt-local.service rather than being enabled like that. In any case it runs too late to be of value for backup purposes.

Work like a charm.
Thread is closed

Thank you very much indeed.

You’re welcome :slight_smile: