linux_wiki:systemd_service_script

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
linux_wiki:systemd_service_script [2016/01/31 00:47]
billdozor [Implementation]
linux_wiki:systemd_service_script [2018/03/23 14:52]
billdozor
Line 6: Line 6:
  
 **Checklist** **Checklist**
-  * Enterprise Linux 7+  * Distro(s): Enterprise Linux 7
  
 ---- ----
Line 23: Line 23:
   * Copy to /etc/systemd/system/<name-of-service>.service   * Copy to /etc/systemd/system/<name-of-service>.service
   * Start your service<code bash>systemctl start <name-of-service></code>   * Start your service<code bash>systemctl start <name-of-service></code>
 +    * **Note: This will fail if the ExecStart script/program does not have the executable permissions or if the EnvironmentFile config file does not exist.**
  
 ---- ----
Line 34: Line 35:
  
 [Service] [Service]
 +Type=simple
 EnvironmentFile=/etc/myprog.d/config EnvironmentFile=/etc/myprog.d/config
 ExecStart=/usr/local/bin/myprog.sh ExecStart=/usr/local/bin/myprog.sh
Line 41: Line 43:
 WantedBy=multi-user.target WantedBy=multi-user.target
 </code> </code>
-  * Description => Displays near the top of output on "systemctl status myprog.service" +  * Section: Unit 
-  * After => can be any valid ".target" unit +    * Description => Displays near the top of output on "systemctl status myprog.service" 
-    * See all: systemctl -t target --all +    * After => can be any valid systemd unit (.target, .service, etc) 
-  * EnvironmentFile => Configuration file to load +      * **Multiple units are space separated** 
-  * ExecStart => Executable to start +      * Another common use target is: After=network.target 
-  * Restart => Auto restarts the program if an un-handled exit error occurs +      * See all: systemctl -t target --all 
-  * WantedBy => If enabled, start under which target+  * Section: Service 
 +    * Type => The process and daemon behavior 
 +      * simple -> default if Type is not set, but ExecStart is. Main process specified in ExecStart line. 
 +      * forking -> service forks a child process and the parent process exits 
 +      * oneshot -> default if Type and ExecStart are not set. Process is short lived, systemd will wait for the process to exit before continuting with other units. 
 +      * notify -> service will issue a notification when it has finished starting. Systemd will wait before processing other units 
 +    * EnvironmentFile => Configuration file to load 
 +    * ExecStart => Executable to start 
 +    * Restart => Auto restarts the program if an un-handled exit error occurs 
 +  * Section: Install 
 +    * WantedBy => If enabled, start under which target 
 + 
 +----
  
 ===== Example: Test Script ===== ===== Example: Test Script =====
Line 53: Line 67:
 This is the script that was used to test the custom systemd service unit file. This is the script that was used to test the custom systemd service unit file.
  
 +**NOTE:** The script being launched MUST have the "#!/bin/bash" line first or the unit file will fail. (Cannot have a standard comment line as the first line)
 +
 +\\
 /usr/local/bin/myprog.sh /usr/local/bin/myprog.sh
 <code bash> <code bash>
Line 65: Line 82:
 done done
 </code> </code>
 +  * **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
 +   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
 +</code>
 +
 +----
 +
 +==== 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]: 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...
 +</code>
 +
 +----
 +
 +===== Example: Docker Compose =====
 +
 +Putting docker-compose commands into a service.
 +
 +/etc/systemd/system/docker-compose.service
 +<code bash>
 +[Unit]
 +Description=Docker Compose Containers
 +Requires=docker.service
 +After=docker.service
 +
 +[Service]
 +Type=oneshot
 +RemainAfterExit=yes
 +ExecStart=/usr/local/bin/docker-compose -f /data/docker/docker-compose.yml up -d
 +ExecStop=/usr/local/bin/docker-compose -f /data/docker/docker-compose.yml down
 +
 +[Install]
 +WantedBy=multi-user.target
 +</code>
 +
 +----
 +
 +====== Override File ======
 +
 +Override files are used when you want to modify part or all of a RPM/system provided systemd unit file. Use over rides instead of directly modifying system unit files, as they could be reverted upon next related package update.
 +
 +System Provided unit files: /usr/lib/systemd/system/
 +
 +===== Override Example: docker service =====
 +
 +System provided file to over ride: /usr/lib/systemd/system/docker.service
 +
 +  * Create a directory to contain the over ride file (named after the original unit file name)<code bash>mkdir /etc/systemd/system/docker.service.d/</code>
 +  * Create the over ride file<code bash>/etc/systemd/system/docker.service.d/docker.conf
 +
 +[Service]
 +EnvironmentFile=
 +EnvironmentFile=-/etc/sysconfig/docker
 +ExecStart=
 +ExecStart=/usr/bin/dockerd $OPTIONS</code>
 +    * Each variable with a blank assignment ('EnvironmentFile=') clears that variable. If you did not put the assignment to nothing line, the variables would be appended to the built in values.
 +
 +  * Reload the systemd daemon for the override file to be picked up<code bash>systemctl daemon-reload</code>
 +
 +  * Restart the service
 +
 +===== Override Example: rpcbind service =====
 +
 +System provided file to over ride: /usr/lib/systemd/system/rpcbind.service
 +
 +  * Create a directory to contain the over ride file (named after the original unit file name, but add a ".d" for directory)<code bash>mkdir /etc/systemd/system/rpcbind.service.d/</code>
 +  * Create the over ride file (NAME.conf)<code bash>/etc/systemd/system/rpcbind.service.d/rpcbind.conf
 +
 +[Service]
 +ExecStart=
 +ExecStart=/sbin/rpcbind $RPCBIND_ARGS</code>
 +    * Each variable with a blank assignment ('EnvironmentFile=') clears that variable. If you did not put the assignment to nothing line, the variables would be appended to the built in values.
 +
 +  * Reload the systemd daemon for the override file to be picked up<code bash>systemctl daemon-reload</code>
 +
 +  * Restart the service
 +
 +----
 +
  • linux_wiki/systemd_service_script.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)