linux_wiki:systemd_service_script

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>
    • Note: This will fail if the ExecStart script/program does not have the executable permissions or if the Environment config file does not exist.

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

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
  • Note: Ensure this script is executable, or the service will fail to start.

[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

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]: Welcome to my program
Jan 24 10:30:08 server1.local logger[17605]: /usr/local/bin/myprog.sh has started on Sun Jan 24 10:30:08 CST 2016
Jan 24 10:30:08 server1.local logger[17606]: /usr/local/bin/myprog.sh is still running...
Jan 24 10:30:38 server1.local logger[17683]: /usr/local/bin/myprog.sh is still running...
Jan 24 10:31:08 server1.local logger[17693]: /usr/local/bin/myprog.sh is still running...

  • linux_wiki/systemd_service_script.1454297849.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)