How to make custom script that runs automatically during boot up in RHEL7
https://access.redhat.com/solutions/1163283
SOLUTION 已验证 - 已更新 2019年三月14日06:49 -
环境
- Red Hat Enterprise Linux 7
问题
- How to make custom script that runs automatically during boot up in RHEL7?
- In RHEL 5 and 6, we were using automatic startup feature of RHEL through /etc/rc.d/init.d.
决议
NOTE: Red Hat does not support implementation of custom scripts, including custom systemd startup scripts. This article is provided as a how-to and Red Hat will not troubleshoot any issues after the implementation of the steps provided here. For more information please see the Production Support Scope of Coverage
- Related solutions:
+ How to configure a command, script, or daemon to run after boot has finished in RHEL 7
+ How to allow a custom service script in RHEL7 to run in the foreground and accept user input
+ How to configure a systemd service in RHEL7 to run as a custom user or group
-
Create a new service unit file at
/etc/systemd/system/example.service1 with content based on the following[Unit] Description=Example Service Script description goes here After=network.target [Service] Type=simple ExecStart=/usr/local/sbin/example.sh TimeoutStartSec=0 [Install] WantedBy=default.targetOptionally, consult footnotes for explanation:
After=2Type=3TimeoutStartSec=4 -
Create the example shell script mentioned in the unit file's
ExecStartdirective, e.g., save the following to/usr/local/sbin/example.sh, making sure the file is executable#!/bin/bash z=0 for i in {1..5}; do sleep 1m ((z++)) wall example.sh: finished minute ${z} done wall example.sh: COMPLETELY FINISHED -
Reload the
systemdprocess to consider newly createdexample.serviceOR every time whenexample.servicegets modified.# systemctl daemon-reload -
Enable the service and then reboot to test
# systemctl enable example.service # reboot
-
The unit file should be located in either
/usr/lib/systemd/system/or/etc/systemd/system/. See UNIT LOAD PATH section inman systemd.unit. ↩ -
After=If the script needs any other system facilities (networking, etc), modify the[Unit]section to include appropriateAfter=,Wants=, orRequires=directives, as described in:man systemd.unit. ↩ -
Type=SwitchType=simpleforType=idlein the[Service]section to delay execution of the script until all other jobs are dispatched (seeman systemd.servicefor even more choices -- e.g.,Type=oneshotcan be useful in concert with other services). ↩ -
TimeoutStartSec=When a service doesn't signal start-up completion withinTimeoutStartSec, systemd considers the service failed; for long-running shell scripts it is essential to modifyTimeoutStartSecor disable the timeout logic altogether as above, withTimeoutStartSec=0. Seeman systemd.servicefor more details. ↩
本文介绍如何在RHEL7中创建自定义脚本,使其在系统启动时自动运行。不同于RHEL5和6使用/etc/rc.d/init.d的方式,RHEL7采用systemd进行服务管理。文章详细说明了创建service单元文件、shell脚本及systemd进程重载等步骤。
1154

被折叠的 条评论
为什么被折叠?



