The systemd facility replaces the older System-V initialization scripts from earlier releases. The systemd is an event-driven facility which allows non-dependent subsystems to be started, controlled, or stopped in parallel. Here we explain how to add a custom script to the systemd facility.


1. Write And Debug The Custom Script


Typically a systemd script is written as a shell script. Begin by writing your custom script using the normal conventions. We will call our script and is straightforward:


echo "I am a custom script" > /var/tmp/script.out
echo "The script was run at : `date`" >> > /var/tmp/script.out


The script must be executable.

# chmod 0755 /var/tmp/


2. Describe The Custom Script To systemd


With the script written and tested manually, the script is ready to be described to the systemd system. To do this, a [name].service file is needed. The syntax uses the INI format commonly used for configuration files. Continuing our example, we need a my-custom-script.service file. The executable will run exactly once for each time the service is started. The service will not be started until the networking layer is up and stable.


Create a new service unit file at /etc/systemd/system/my-custom-script.service with below content. The name of the service unit is user defined and can be any name of your choice.


 # This is my-custom-script.service, which describes the file
Description=This is executed on shutdown or reboot
DefaultDependencies=no                                                                   # (if network is required before running the script)                        # Defines the order in which units are stoped. #(REQUIRED)

Type=oneshot                                                                               # enables specifying multiple custom commands that are then executed sequentially. (REQUIRED)
RemainAfterExit=true                                                                       # required by the oneshot setting (REQUIRED)
Environment=ONE='one' "TWO='2"                                                             # you can set some environment variables, that may be necessary to pass as arguments
ExecStart=/bin/true                                                                        # because is a shutdown script nothing is done when this service is started
ExecStop=/bin/bash /var/tmp/ ${ONE} ${TWO}                              # < --*********** change to the script full path ************ (REQUIRED)
TimeoutStopSec=1min 35s                                                                    # Configures the time to wait for stop. 

[Install]                                                                 # When this unit is enabled, the units listed in WantedBy gain a Want dependency on the unit. (REQUIRED)


3. Enable The Script For Future Reboots


Similar to the chkconfig from earlier versions, the service must be enabled. Since a new service was added, notify the systemd daemon to reconfigure itself:


# systemctl enable my-custom-script.service
# systemctl daemon-reload


Was this answer helpful? 0 Users Found This Useful (0 Votes)