Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
linux_wiki:systemd_service_script [2015/12/21 23:09] billdozor created |
linux_wiki:systemd_service_script [2019/05/25 23:50] (current) |
||
---|---|---|---|
Line 6: | Line 6: | ||
**Checklist** | **Checklist** | ||
- | * Enterprise Linux 7 | + | * Distro(s): |
---- | ---- | ||
- | ====== Unit File Locations | + | ====== Unit File Reference |
- | TODO | + | * / |
+ | * / | ||
- | Example | + | ---- |
+ | |||
+ | ====== Implementation ====== | ||
+ | |||
+ | * Download the code | ||
+ | * Modify each section to make sense for your service | ||
+ | * Copy to / | ||
+ | * Start your service< | ||
+ | * **Note: This will fail if the ExecStart script/ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ====== The Service Unit File ====== | ||
+ | |||
+ | <code bash myprog.service> | ||
+ | [Unit] | ||
+ | Description=My Awesome Program | ||
+ | After=syslog.target | ||
+ | |||
+ | [Service] | ||
+ | Type=simple | ||
+ | EnvironmentFile=/ | ||
+ | ExecStart=/ | ||
+ | Restart=on-abort | ||
+ | |||
+ | [Install] | ||
+ | WantedBy=multi-user.target | ||
+ | </ | ||
+ | * Section: Unit | ||
+ | * Description => Displays near the top of output on " | ||
+ | * After => can be any valid systemd unit (.target, .service, etc) | ||
+ | * **Multiple units are space separated** | ||
+ | * Another common use target is: After=network.target | ||
+ | * See all: systemctl -t target --all | ||
+ | * 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: | ||
+ | |||
+ | This is the script that was used to test the custom systemd service unit file. | ||
+ | |||
+ | **NOTE:** The script being launched MUST have the "# | ||
+ | |||
+ | \\ | ||
+ | / | ||
<code bash> | <code bash> | ||
- | Example code here | + | # |
+ | |||
+ | 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]: | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Example: Docker Compose ===== | ||
+ | |||
+ | Putting docker-compose commands into a service. | ||
+ | |||
+ | / | ||
+ | <code bash> | ||
+ | [Unit] | ||
+ | Description=Docker Compose Containers | ||
+ | Requires=docker.service | ||
+ | After=docker.service | ||
+ | |||
+ | [Service] | ||
+ | Type=oneshot | ||
+ | RemainAfterExit=yes | ||
+ | ExecStart=/ | ||
+ | ExecStop=/ | ||
+ | |||
+ | [Install] | ||
+ | WantedBy=multi-user.target | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Example: Apache HTTPD Compiled ===== | ||
+ | |||
+ | Creating a service unit file for a locally compiled and installed Apache web server. | ||
+ | |||
+ | \\ | ||
+ | / | ||
+ | <code bash> | ||
+ | [Unit] | ||
+ | Description=Apache Web Server | ||
+ | After=network.target remote-fs.target nss-lookup.target | ||
+ | |||
+ | [Service] | ||
+ | Type=forking | ||
+ | PIDFile=/ | ||
+ | ExecStart=/ | ||
+ | ExecStop=/ | ||
+ | ExecReload=/ | ||
+ | PrivateTmp=true | ||
+ | LimitNOFILE=infinity | ||
+ | |||
+ | [Install] | ||
+ | WantedBy=multi-user.target | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | Reload systemd< | ||
+ | |||
+ | \\ | ||
+ | Enable/ | ||
+ | systemctl start httpd-local</ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ====== 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: / | ||
+ | |||
+ | ===== Override Example: docker service ===== | ||
+ | |||
+ | System provided file to over ride: / | ||
+ | |||
+ | * Create a directory to contain the over ride file (named after the original unit file name)< | ||
+ | * Create the over ride file< | ||
+ | |||
+ | [Service] | ||
+ | EnvironmentFile= | ||
+ | EnvironmentFile=-/ | ||
+ | ExecStart= | ||
+ | ExecStart=/ | ||
+ | * Each variable with a blank assignment (' | ||
+ | |||
+ | * Reload the systemd daemon for the override file to be picked up<code bash> | ||
+ | |||
+ | * Restart the service | ||
+ | |||
+ | ===== Override Example: rpcbind service ===== | ||
+ | |||
+ | System provided file to over ride: / | ||
+ | |||
+ | * Create a directory to contain the over ride file (named after the original unit file name, but add a " | ||
+ | * Create the over ride file (NAME.conf)< | ||
+ | |||
+ | [Service] | ||
+ | ExecStart= | ||
+ | ExecStart=/ | ||
+ | * Each variable with a blank assignment (' | ||
- | ===== Sub-section Title ===== | + | * Reload the systemd daemon for the override file to be picked up<code bash> |
- | Section 1, sub-content. | + | * Restart the service |
---- | ---- | ||