This is an old revision of the document!
Systemd Service Script
General Information
Creating a systemd until file (service script).
Checklist
- Enterprise Linux 7
Unit File Reference
- /usr/lib/systemd/system ⇒ system unit configuration files (default with system)
- /etc/systemd/system ⇒ additional configuration files (downloaded or custom)
Implementation
- Download the code
- Modify each section to make sense for your service
- Copy to /etc/systemd/system/<name-of-service>.service
- Start your service
systemctl start <name-of-service>
The Service Unit File
- myprog.service
[Unit] Description=My Awesome Program After=syslog.target [Service] EnvironmentFile=/etc/myprog.d/config ExecStart=/usr/local/bin/myprog.sh Restart=on-abort [Install] WantedBy=multi-user.target
- Description ⇒ Displays near the top of output on “systemctl status myprog.service”
- After ⇒ can be any valid “.target” unit
- See all: systemctl -t target –all
- EnvironmentFile ⇒ Configuration file to load
- ExecStart ⇒ Executable to start
- Restart ⇒ Auto restarts the program if an un-handled exit error occurs
- WantedBy ⇒ If enabled, start under which target
Example: Test Script
This is the script that was used to test the custom systemd service unit file.
/usr/local/bin/myprog.sh
#!/bin/bash echo "Welcome to my program" logger -p info "$0 has started on $(date)" while true; do logger -p info "$0 is still running..." sleep 30 done
Example: Service Status
[root@server1 system]# systemctl start myprog.service [root@server1 system]# systemctl status myprog.service ● myprog.service - My Awesome Program Loaded: loaded (/etc/systemd/system/myprog.service; disabled; vendor preset: disabled) Active: active (running) since Sun 2016-01-24 10:30:08 CST; 6s ago Main PID: 17602 (myprog.sh) CGroup: /system.slice/myprog.service ├─17602 /bin/bash /usr/local/bin/myprog.sh └─17608 sleep 30 Jan 24 10:30:08 server1.local systemd[1]: Started My Awesome Program. Jan 24 10:30:08 server1.local systemd[1]: Starting My Awesome Program... Jan 24 10:30:08 server1.local myprog.sh[17602]: Welcome to my program