【问题标题】:Run shell script on reboot重新启动时运行 shell 脚本
【发布时间】:2020-01-20 17:45:36
【问题描述】:

尝试了两种方法,似乎都不起作用:

crontab -e:

@reboot sleep 60;/home/linuxbox/script.sh

并在/etc/systemd/system/script.service中创建了一个服务:

[Unit]
Description=a generic service to run on reboot

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/bin/bash /home/linuxbox/script.sh
Type=simple
User=linuxbox
Group=linuxbox
WorkingDirectory=/home/linuxbox

我用systemctl daemon-reload跟进。

目前不知道出了什么问题——感谢任何帮助。

【问题讨论】:

  • 检查sudo journalctl -u script查看服务日志。
  • -- No entries --

标签: linux cron systemd reboot


【解决方案1】:

你启用了 cron 吗?

您可以启用并启动它

sudo systemctl enable cron.service   

【讨论】:

  • 返回Unit file crond.service does not exist. 但是当我运行service --status-all 时,我确实得到了[ + ] cron 条目?
  • 没有crond.service,而是称为cron.service(没有尾随的“d”)。但是,我怀疑 cron 尚未在系统上运行,因为这很可能会破坏各种事情)
【解决方案2】:

ExecStart 命令不应该是

/bin/bash /home/linuxbox/script.sh

但应该是

/bin/bash -c "/home/linuxbox/script.sh".

【讨论】:

  • 似乎没有什么不同。 :(
【解决方案3】:

一旦您创建了 script.service 单元,您当然必须启用它:

systemctl enable script.service

(这可能看起来很明显,但在第一个问题中你只提到你运行systemctl daemon-reload 这还不够)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-03
    • 2019-12-13
    • 1970-01-01
    • 2018-06-11
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    相关资源
    最近更新 更多