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

时间:2018-01-08 20:26:00

标签: linux bash cron rhel

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

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

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

3 个答案:

答案 0 :(得分:3)

我只会在重启脚本的开头sleep 600。当然,可能有一种更“专家”的方式,但它会起作用。

答案 1 :(得分: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用户进行管理。

答案 2 :(得分:0)

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