我想使用 var 作为 crontab 中的值之一。我正在使用 Ubuntu
#example
rand=$(( ( RANDOM % 10 ) + 1 ))
rand * * * * /someScript.sh
最佳答案
我从未听说过crontab
的文件中有这种可能性。而且我不确定您要达到什么目的。能否详细说明一下?
来自 cron
的手册,
cron then wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute.
但是你可以做的是有一个每分钟执行的脚本,在该脚本中你可以生成一个随机数和一个阈值来决定是否执行你的 someScript.sh
例如 cron 看起来像
*/1 * * * * /gateScript.sh
和 gateScript.sh 想要:
#!/bin/bash
rand=${RANDOM}
threshold=16384 #middle value of possible $RANDOM outputs
if [ "${rand}" -gt "${threshold}" ]; then
exec someScript.sh
fi
参见 here用于更精细的“随机”数字生成。
关于ubuntu - 如何在 crontab 中使用变量作为执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43030606/