Differences
This shows you the differences between two versions of the page.
linux_wiki:systemd_service_script [2016/01/31 22:36] billdozor [Example: Test Script] |
linux_wiki:systemd_service_script [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Systemd Service Script ====== | ||
- | |||
- | **General Information** | ||
- | |||
- | Creating a systemd until file (service script). | ||
- | |||
- | **Checklist** | ||
- | * Enterprise Linux 7 | ||
- | |||
- | ---- | ||
- | |||
- | ====== Unit File Reference ====== | ||
- | |||
- | * / | ||
- | * / | ||
- | |||
- | ---- | ||
- | |||
- | ====== Implementation ====== | ||
- | |||
- | * Download the code | ||
- | * Modify each section to make sense for your service | ||
- | * Copy to / | ||
- | * Start your service< | ||
- | |||
- | ---- | ||
- | |||
- | ====== The Service Unit File ====== | ||
- | |||
- | <code bash myprog.service> | ||
- | [Unit] | ||
- | Description=My Awesome Program | ||
- | After=syslog.target | ||
- | |||
- | [Service] | ||
- | EnvironmentFile=/ | ||
- | ExecStart=/ | ||
- | Restart=on-abort | ||
- | |||
- | [Install] | ||
- | WantedBy=multi-user.target | ||
- | </ | ||
- | * Description => Displays near the top of output on " | ||
- | * After => can be any valid " | ||
- | * 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. | ||
- | |||
- | / | ||
- | <code bash> | ||
- | #!/bin/bash | ||
- | |||
- | echo " | ||
- | logger -p info "$0 has started on $(date)" | ||
- | |||
- | while true; do | ||
- | logger -p info "$0 is still running..." | ||
- | sleep 30 | ||
- | done | ||
- | </ | ||
- | * **Note: Ensure this script is executable, or the service will fail to start.** | ||
- | |||
- | ---- | ||
- | |||
- | ==== Example: Service Status ==== | ||
- | |||
- | <code bash> | ||
- | [root@server1 system]# systemctl start myprog.service | ||
- | [root@server1 system]# systemctl status myprog.service | ||
- | ● myprog.service - My Awesome Program | ||
- | | ||
- | | ||
- | Main PID: 17602 (myprog.sh) | ||
- | | ||
- | | ||
- | | ||
- | |||
- | 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]: | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== Example: Logs ==== | ||
- | |||
- | <code bash> | ||
- | journalctl -f | ||
- | -- Logs begin at Fri 2015-12-25 22:36:05 CST. -- | ||
- | Jan 24 10:30:08 server1.local systemd[1]: Starting My Awesome Program... | ||
- | Jan 24 10:30:08 server1.local myprog.sh[17602]: | ||
- | Jan 24 10:30:08 server1.local logger[17605]: | ||
- | Jan 24 10:30:08 server1.local logger[17606]: | ||
- | Jan 24 10:30:38 server1.local logger[17683]: | ||
- | Jan 24 10:31:08 server1.local logger[17693]: | ||
- | </ | ||
- | |||
- | ---- | ||