How to configure a command, script, or daemon to run after boot has finished in RHEL 7

本文介绍如何在RHEL7系统中使用systemd配置命令、脚本或守护进程在所有其他服务脚本和systemd初始化任务完成后运行。通过创建自定义的systemd服务单元并设置特定属性,可以实现这一需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How to configure a command, script, or daemon to run after boot has finished in RHEL 7

https://access.redhat.com/solutions/1751263

 SOLUTION 已验证 - 已更新 2019年四月25日20:34 - 

English 

环境

  • Red Hat Enterprise Linux (RHEL) 7
  • systemd

问题

  • How can we configure a command, script, application, or daemon to run after all other service scripts and systemd init tasks?

决议


Disclaimer: The following information has been provided by Red Hat, but is outside the scope of the posted Service Level Agreements and support procedures. Red Hat does not support the 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. The intent of this article is to provide information to accomplish the system's needs. Use of the information in this article at the user's own risk.


1. Create a new /etc/systemd/system/very-last.service file with the following content:

Raw

[Unit]
Description=Very last service
After=default.target

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/very-last

[Install]
WantedBy=default.target

2. Reload systemd daemon:

Raw

# systemctl daemon-reload

3. Enable the newly created service very-last:

Raw

# systemctl enable very-last

4. Create the custom script specified in ExecStart=, in this case /usr/local/sbin/very-last:

Raw

# echo '#!/bin/bash' > /usr/local/sbin/very-last

5. Set the permissions to it:

Raw

# chmod 700 /usr/local/sbin/very-last

Note: if SELinux is enforcing, run restorecon -Fvvv /usr/local/sbin/very-last to set the context.

6. Edit /usr/local/sbin/very-last to include desired commands

7. Reboot the system to test it.

8. After boot, use systemd-analyze plot > file.svg to generate an image of the boot process for inspection. There could be one or two short-lived services starting after very-last.service. If that's a problem, modify /etc/systemd/system/very-last.service to set Type=idle and if the script could take more than 90 secs to run, also set TimeoutStartSec=0.

根源

With the switch to systemd in RHEL 7, rc.local no longer runs near the end of the boot process.
Instead of using rc.local, create a custom service unit configured with After=default.target and WantedBy=default.target to launch a script, command, or series of commands.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值