bash 每天随机运行一次 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16873713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running a shell script once a day at random time
提问by Nemo
Need run a shell script once a day at random time. (so once every day between 00:00-23:59).
需要每天随机运行一次 shell 脚本。(所以每天 00:00-23:59 之间一次)。
I know the sleepcommand, and the crontoo, but
我知道sleep命令,也知道cron,但是
- the
cronhas not random times - and the
sleepsolution - not very nice - my idea is launch the script every midnight and sleep random time at the start of the script.
- 在
cron还没有随机时间 - 和
sleep解决方案 - 不是很好 - 我的想法是每午夜启动脚本并在脚本开始时随机睡眠。
Is here something more elegant?
这里有更优雅的东西吗?
回答by jm666
If you have the atcommand, you can combinte the cronand the at.
如果你有at命令,你可以组合cron和at。
Run from a cron every midnight the next script:
每个午夜从 cron 运行下一个脚本:
#!/bin/bash
script="/tmp/script.sh" #insert the path to your script here
min=$(( 24 * 60 ))
rmin=$(( $RANDOM % $min ))
at -f "$script" now+${rmin}min
The above will run the atcommand every midnight and will execute your script at random time . You should check your crontab how often is the atruncommand started. (The atrunruns the commands stored with the at)
以上将在at每个午夜运行命令,并随机执行您的脚本。您应该检查您的 crontabatrun命令启动的频率。(atrun运行与 一起存储的命令at)
The main benefit in comparison with the sleepmethod: this "survives" the system reboot.
与该sleep方法相比的主要好处:这“幸存”了系统重新启动。
回答by e2-e4
The best alternative to cronis probably at
最好的选择cron可能是at
- See
atman page
- 参见
at手册页
Usually, atreads commands from standard input, but you can give a file of jobs with -f.
通常,at从标准输入读取命令,但您可以提供带有-f.
Time wise, you can specify many formats. Maybe in your case the most convenient would be
时间方面,您可以指定多种格式。也许在你的情况下最方便的是
at -f jobs now + xxx minutes
at -f jobs now + xxx minutes
where your scripts gives xxx as a random value from 1to 1440(1440 minutes in a day), and jobscontains the commands you want to be executed.
您的脚本将 xxx 作为随机值从1到1440(一天中的 1440 分钟)给出,并jobs包含您要执行的命令。
回答by glenn Hymanman
I would simply launch you script at midnight, and sleepfor a random time between 0 and 86400 seconds. Since my bash's $RANDOMreturns a number between 0 and 32767:
我会简单地在午夜启动你的脚本,并且sleep在 0 到 86400 秒之间的随机时间。由于我的 bash$RANDOM返回一个介于 0 和 32767 之间的数字:
sleep $(( ($RANDOM % 1440)*60 + ($RANDOM % 60) ))
回答by bartimar
You can use cron to launch bash script, which generates pseudorandom timestamp and gives it to unix program at
您可以使用 cron 启动 bash 脚本,该脚本生成伪随机时间戳并将其提供给 unix 程序 at
I see you are familiar with bash and cron enough, so atwill be a piece of cake for you. Documentation as always "man at" or you can try wiki
我看你对 bash 和 cron 已经足够熟悉了,所以at对你来说是小菜一碟。文档总是“man at”,或者你可以试试wiki
回答by user1666959
Nothing prevents you from running sed to patch your crontab as the last thing your program does and just changing the next start time. I wouldn't sleep well though.
没有什么可以阻止您像程序所做的最后一件事一样运行 sed 来修补您的 crontab,而只是更改下一次启动时间。不过我也睡不好。

