【问题标题】:Automatically starting oneshot service on bootup在启动时自动启动 oneshot 服务
【发布时间】:2018-09-20 05:12:30
【问题描述】:

我有一个 oneshot 服务,我想在 rhel 7.4 上启动它。

我了解"chkconfig --add" 是实现此目的的一种方法。但它要求启动脚本存在于

/etc/init.d 目录

对于非 oneshot 服务,我了解可以在 systemd 服务文件中使用“Restart=”选项。

如果我尝试为 oneshot 服务设置 Restart=always,它不起作用。 Oneshot 服务需要"Restart=no".

有没有什么方法可以在启动时启动 oneshot 服务而无需在其中添加脚本

/etc/init.d 目录

【问题讨论】:

  • 我添加了一个答案。注意Typeoneshot

标签: systemd


【解决方案1】:

首先,由于我们没有提到您的 Systemd 服务,这里举个例子:

示例文件名:

/etc/systemd/system/my-startup-script.service

示例内容:

[Unit]
Description=My startup script
#After=network.target

[Service]
Type=oneshot
ExecStart=/root/my-startup-script.sh
RemainAfterExit=true

[Install]
WantedBy=multi-user.target

之后,只需启用它:

systemctl daemon-reload
systemctl enable my-startup-script

这就是我的朋友!

【讨论】:

    【解决方案2】:

    “systemctl enable”负责启动任何服务,无论是 oneshot 还是其他。

    【讨论】:

      【解决方案3】:

      我认为您需要在 /etc/init.d 上添加服务并使用 chkconfig 在启动时始终初始化服务,“chkconfig on”在计算机启动时启动服务。 您也可以通过脚本 cron 来初始化服务。 参见:cat /etc/crontab

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-25
        • 2019-08-23
        • 2016-04-28
        • 2017-11-24
        • 1970-01-01
        • 2020-11-28
        • 2011-12-03
        相关资源
        最近更新 更多