代码之家  ›  专栏  ›  技术社区  ›  Naveen Balasubramanian

系统启动10分钟后运行bash脚本

  •  4
  • Naveen Balasubramanian  · 技术社区  · 7 年前

    我正在尝试在系统启动10分钟后和每次重新启动时运行bash脚本。我计划重新启动crontab,但我不确定有两件事

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

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

    3 回复  |  直到 7 年前
        1
  •  5
  •   user3243135    7 年前

    我只是 sleep 600 在重新启动脚本的开头。当然,可能有一种更“专业”的方法,但它会奏效。

        2
  •  4
  •   JawguyChooser    7 年前

    我认为您的问题可能更适合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
  •  0
  •   iamauser    7 年前

    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