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
Situation
Resolution
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.targetNOTE: 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
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: 24-Jul-2020
- Modified Date:17-Nov-2023
-
- SUSE Linux Enterprise Server
For questions or concerns with the SUSE Knowledgebase please contact: tidfeedback[at]suse.com