【问题标题】:Running bash script 10 minutes after the system start系统启动 10 分钟后运行 bash 脚本
【发布时间】:2018-06-17 20:39:34
【问题描述】:

我尝试在系统启动 10 分钟后和每次重新启动时运行 bash 脚本。我本来打算用 crontab 的@reboot,但我不确定两件事

  • 是在系统首次启动时运行还是仅在重新启动时运行。
  • 如何在重新启动后将运行延迟 10 分钟。

哪种表达方式最适合我的情况?请注意,我无法运行“at”或系统计时器来完成此操作,因为我们无法访问两者。我正在开发 RHEL 7..

【问题讨论】:

    标签: linux bash cron rhel


    【解决方案1】:

    我会在重启脚本的开头sleep 600。当然,可能有一种更“专业”的方式来做这件事,但它会奏效。

    【讨论】:

    • 那是一小时,但 10 分钟是 600 秒。但是+1;就是这样。
    • 哦,是的。但是@reboot 会在第一次启动时触发脚本吗?
    • 如果您设法安装了@reboot 条目,那么第一次启动已经发生。无论如何,Cron 并不在乎。 “重启”在技术上有点误导,因为它真的会在第一次启动后运行(cron 无法确定最新的启动是否是 re-boot)。
    • 哎呀^_^信不信由你我有数学学位。
    【解决方案2】:

    我认为您的问题可能更适合 Unix 和 Linux 堆栈交换,因为我在那里找到了两个直接解决您的问题的答案:

    https://unix.stackexchange.com/questions/57852/crontab-job-start-1-min-after-reboot

    基本上,您可以随时将sleep 600 添加到您的 cronjob 调用的开头。

    至于您是否应该运行 cronjob 与 init 脚本:

    https://unix.stackexchange.com/questions/188042/running-a-script-during-booting-startup-init-d-vs-cron-reboot

    有一些细微的差别,但基本上,您的 cron @reboot 将在每次系统启动时运行,并且作为非 root 用户可能更容易管理。

    【讨论】:

      【解决方案3】:

      rc-local.service 会更好地满足您在 EL7 系统上的需求。

        systemctl status rc-local.service
        ● rc-local.service - /etc/rc.d/rc.local Compatibility
          Loaded: loaded (/usr/lib/systemd/system/rc-local.service; static; vendor preset: disabled)
         Active: inactive (dead)
      

      您需要将可以以任意延迟运行的脚本放入文件中,/etc/rc.d/rc.local,例如,

      sleep 600 && /usr/local/bin/myscript.sh
      

      或者您可以在脚本中设置延迟。

      # Give exe permission to the local script as well as `rc.local`
      chmod a+x /usr/local/bin/myscript.sh
      chmod a+x /etc/rc.d/rc.local
      
      # Enable the service. Note the service name has a `-` compared `.` in the file.
      systemctl enable rc-local.service
      

      【讨论】:

        猜你喜欢
        • 2014-03-27
        • 1970-01-01
        • 2020-11-10
        • 2011-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-23
        • 2020-04-20
        相关资源
        最近更新 更多