ubuntu - 如何在 crontab 中使用变量作为执行时间

标签 ubuntu cron

我想使用 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/

相关文章:

ubuntu - 命令 dnx Run 和 dnx web 的不当行为

python - Py.test 收集阶段需要很长时间

Python-crontab 导入错误

linux - 如何在简单脚本上使用 cron

Android,搭建Linux编译环境,libgl1-mesa-glx :i386 package have unmet dependencies

linux - phpmyadmin, neginx error.log - 检查组 www-data 有读取权限和 open_basedir

python - ImportError : No module named geoip2. 数据库

PHP 错误行 1 : `<?php ?>'

linux - Linux shell 循环中提示输入

azure - Azure WebJobs 中的 NCRONTAB 表达式