简体   繁体   English

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

[英]Running bash script 10 minutes after the system start

I'm trying to run a bash script 10 minutes after my system startup and on every reboot. 我试图在系统启动后10分钟以及每次重新启动时运行bash脚本。 I was planning to the @reboot of crontab, but I'm not sure of two things 我本来打算使用crontab的@reboot,但是我不确定两件事

  • Whether it will run on the first system start or only on reboot. 它是在首次系统启动时运行还是仅在重新启动时运行。
  • How to delay the run by 10 minutes after the reboot. 重新启动后如何将运行延迟10分钟。

What expression would suit my situation the best? 哪种表达最适合我的情况? Please note that I can't run 'at' or system timer to accomplish this as both are not accessible to us. 请注意,我无法运行“ at”或系统计时器来完成此操作。 I'm working on the RHEL 7.. 我正在使用RHEL 7。

I would just sleep 600 at the beginning of your reboot script. 在重新启动脚本的开头,我只会sleep 600小时。 Sure, there's probably a more "expert" way of doing it, but it'll work. 当然,可能有更“专家”的方式来做到这一点,但它会起作用。

I think your question may be more appropriate on the Unix and Linux stack exchange, because I found two answers over there which directly address your question: 我认为您的问题在Unix和Linux堆栈交换上可能更合适,因为我在那里找到了两个直接解决您问题的答案:

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

Basically you can always just add sleep 600 to the beginning of your cronjob invocation. 基本上,您总是可以在cronjob调用开始时添加sleep 600

As to whether you should be running a cronjob vs an init script: 关于是否应该运行cronjob与init脚本:

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

There are a handful of subtle differences, but basically, your cron @reboot will run each time the system starts and may be more easy to manage as a non-root user. 有一些细微的差别,但是基本上,您的cron @reboot会在每次系统启动时运行,并且作为非root用户可能更易于管理。

rc-local.service would be better for your needs on a EL7 system. 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)

You need to put your script that can run with any amount of delay inside the file, /etc/rc.d/rc.local , eg, 您需要将可以任意延迟运行的脚本放入文件/etc/rc.d/rc.local ,例如,

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

OR you can put delay inside the script. 或者,您可以将延迟放入脚本中。

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM