gpt4 book ai didi

linux - 每次只在一个文件的一行上运行 crontab

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:11:03 25 4
gpt4 key购买 nike

我正在尝试将 crontab 配置为在不同时间执行文件中的不同代码行。我基本上有一个启动一些 java -jar 的 bash 脚本文件。问题是每一行应该在不同的时间执行。我可以将 crontab 配置为在不同时间运行整个脚本,但没有要运行的行。现在,重要的是 bash 文件将只保留一个文件,而不是分解为几个文件。谢谢!

最佳答案

一种方法(通过 cron 传递的命令行参数)

some_script.sh:

if test $1 = 1 ; then

# echo "1 was entered"
java -jar some_file.jar

elif test $1 = 2 ; then

# echo "2 was entered"
java -jar another_file.jar

fi

定时任务示例:

* 1 * * * /bin/bash /home/username/some_script.sh 1
* 2 * * * /bin/bash /home/username/some_script.sh 2

另一种方法(在 bash 脚本中完成小时匹配)

some_script.sh:

hour=$(date +"%H");

if test $hour = 1 ; then

# echo "the hour is 1";
java -jar some_file.jar

elif test $hour = 2 ; then

# echo "the hour is 2";
java -jar another_file.jar

fi

定时任务示例:

* 1 * * * /bin/bash /home/username/some_script.sh
* 2 * * * /bin/bash /home/username/some_script.sh

关于linux - 每次只在一个文件的一行上运行 crontab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36893852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com