SUSE Support

Here When You Need Us

Create a simple systemd service unit file and run a script on boot

This document (000019672) is provided subject to the disclaimer at the end of this document.

Environment

SUSE Linux Enterprise Server 15
SUSE Linux Enterprise Server 12

Situation

A script is needed to run on boot. With SUSE Linux Enterprise Server 12 and above, a systemd service unit file is needed. There are MANY parameters you can add, but here is the most simple version of a unit file:

Resolution

Here is a very simple script to run. For this basic setup we create a simple script that runs once and then is done as part of the boot process. For this example the script is in /usr/sbin/. The script needs to be marked executable.
server1:/root/ # ls -lah /usr/sbin/example.sh
-rwxr-xr-x 1 root root 58 Jul 24 14:14 /usr/sbin/example.sh

server1:/root/ # cat /usr/sbin/example.sh
#!/bin/bash
echo "the script works" >> /tmp/diditwork.txt

Next create a systemd service unit file named test.service. Please note you want this to be in /etc/systemd/system/ and NOT in /usr/lib/systemd/ or it could be written over on the next update.

The key thing to change is the path to your script in your environment. ExecStart=/bin/bash /usr/sbin/example.sh   (The example script created above is /usr/sbin/example.sh)       
server1:/etc/systemd/system # cat /etc/systemd/system/test.service
[Unit]
Description=example systemd service unit file.

[Service]
ExecStart=/bin/bash /usr/sbin/example.sh

[Install]
WantedBy=multi-user.target
NOTE: Depending on the intention, it may be desired to add the "Type=oneshot" option at the start of the [Service] section. The default with the above example would be "Type=simple", basically changing this determines how a service is handled. All the possible options available and their influence on a systemd service however goes beyond the scope of this TID, for additional details see the systemd.service man page.

To reload systemd with this new service unit file, run:
systemctl daemon-reload

Finally to start the script on boot, enable the service with systemd:
systemctl enable test.service

After a reboot, the service file ran the script. The script wrote the output correctly to the output file:
server1:/root/ # cat /tmp/diditwork.txt
the script works

Additional Information

For complete documentation about systemd services, please visit systemd service documentation

Disclaimer

This Support Knowledgebase provides a valuable tool for SUSE customers and parties interested in our products and solutions to acquire information, ideas and learn from one another. Materials are provided for informational, personal or non-commercial use within your organization and are presented "AS IS" WITHOUT WARRANTY OF ANY KIND.

  • Document ID:000019672
  • Creation Date: 17-Nov-2023
  • Modified Date:17-Nov-2023
    • SUSE Linux Enterprise Server

< Back to Support Search

For questions or concerns with the SUSE Knowledgebase please contact: tidfeedback[at]suse.com

SUSE Support Forums

Get your questions answered by experienced Sys Ops or interact with other SUSE community experts.

Support Resources

Learn how to get the most from the technical support you receive with your SUSE Subscription, Premium Support, Academic Program, or Partner Program.

Open an Incident

Open an incident with SUSE Technical Support, manage your subscriptions, download patches, or manage user access.