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.
SUSE Linux Enterprise Server 15
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:
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
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
For questions or concerns with the SUSE Knowledgebase please contact: email@example.com