0

I have a script in /etc/rc.d/init.d/ on a Red Hat 7 system that is provided by a vendor. This script is able to be started and stopped via systemctl, but it appears to not actually be a systemd unit file.

The script depends on a drive being mounted on boot by a systemd unit file. However, this init script tries to start before the mount is finished, so it invariably fails.

I have attempted a hack by adding a line to the beginning of the init script, that causes the script to sleep for 30 seconds before the rest of the script executes: sleep 30.

However, the sleep functionality does not work all the time.

Is there any way to make this init script depend on the systemd mount unit file being completed? Any better ways to accomplish this task than adding a sleep to the beginning of the init script? Thanks.

1
  • Could you convert the initd script to a systemd service file and then utilize the RequiresMountsFor=/path/to/mount or After=yourmount.mount functions?
    – Natolio
    Sep 22, 2022 at 18:08

2 Answers 2

1

SysV init scripts are auto-converted by systemd into systemd Unit files. See man systemd-sysv-generator. You would like to edit the generated Unit to add a suitable dependency for the mount point. You can do this by creating a "drop-in" file with just a few extra lines. If your init file is called, say, /etc/rc.d/init.d/mysysv, then the generated Unit will be called mysysv.service. Enter the command:

sudo systemctl edit mysysv

and you should be in your chosen editor (set env variable EDITOR) on a temporary file. Edit the file to contain something like:

[Unit]
# default timeout of 90 secs for dir to be mounted
JobTimeoutSec=600
RequiresMountsFor=/path/to/mount

and exit the editor cleanly. This creates file /etc/systemd/system/mysysv.service.d/override.conf. When you now start the mysysv Unit, this modification will make the job wait upto 600 seconds until the mount point has something mounted on, before starting. Otherwise it fails with a timeout. The default wait time for a job is a system global value of 90 seconds.

0

here is a bare bone template systemd file

#!/bin/bash

[Unit]
Description=license administration
After=default.target

[Service]
Type=idle
ExecStart=/root/scripts/administration.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target
  • name the above my_administration.service for example
  • locate it in /etc/systemd/system/
  • having permissions root.root and -rw-r--r--
  • then do a systemctl daemon-reload
  • then do a systemctl list-unit-files and you will see my_administration.service listed
  • do a systemctl enable my_administration
  • after boot, given the above syntax, the file /root/scripts/administration.sh will be executed, once the default.target run level is reached, which is graphical.target in my case per systemctl get-default
  • my administration.sh file has permissions root.root and -rwx------; it must be executable obviously and that file I manually create; that's my custom little script to do stuff after linux has fully booted.

look at other systemd service files as a reference

refer to redhat system administrator's guide, Chapter 10. Managing Services with systemd

you want to make use of

[Unit]
Before=
Wants=
After=
Requires=

to get it whatever your ExecStart= file is to happen at a certain time... before other services start and definitely after certain services have started. And not just these four, there are many others which you'll have to look up in the system administrator's guide which you made need to use to make what you want happen happen, those four are just the most commonly used.

you can find all the service files in /usr/lib/systemd/system/ to use as a reference.

also check out: https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files

ALSO: in your script, you also might be able to do something like

if mountpoint -q /bkup ;then /usr/bin/rsnapshot daily; fi

In my /etc/crontab where I do a daily backup by running rsnapshot I first do a mountpoint -q to make sure my 10tb backup is mounted and that I don't backup to an unmounted folder on the root partition and crash my system.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .