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

Running bash script 10 minutes after the system start

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

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

我只是 sleep 600 在你的重启脚本的开头。当然,可能还有更多 "expert" 的方法,但它会起作用。

我认为你的问题在 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 将在每次系统启动时 运行 并且作为非根用户可能更易于管理。

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