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 12
SUSE Linux Enterprise Server 15

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.
sles151:/root/ # ls -lah /usr/sbin/example.sh
-rwxr-xr-x 1 root root 58 Jul 24 14:14 /usr/sbin/example.sh

sles151:/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)       
sles151:/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


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:
sles151:/root/ # cat /tmp/diditwork.txt
the script works

 

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: 22-Sep-2020
  • Modified Date:22-Sep-2020
    • SUSE Linux Enterprise Server

< Back to Support Search

For questions or concerns with the SUSE Knowledgebase please contact: tidfeedback@suse.com

SUSE Support Forums

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

Join Our Community

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.


SUSE Customer Support Quick Reference Guide SUSE Technical Support Handbook Update Advisories
Support FAQ

Open an Incident

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

Go to Customer Center