gpt4 book ai didi

python - 如何在 bash 中的一周中的特定一天只运行一次 python 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:19 25 4
gpt4 key购买 nike

我希望我的 python 脚本只在周二、周五和周日执行,但要注意的是我只希望它执行一次。

while true; do 
# %u day of week (1..7); 1 is Monday
DATE=$(date +%u)

# if DATE, 2 -eq tuesday, 5 -eq friday, 7 -eq sunday
if [ $DATE -eq 2 ] || [ $DATE -eq 5 ] || [ $DATE -eq 7 ]; then
#execute python script
echo "Today is $DATE"
fi

echo $DATE
done

最佳答案

你需要的是定时任务:

首先在 python 脚本的最顶部添加一个 shebang 行。

#!/usr/bin/python (Depends on where your python is: check its path with: $ whereis python)

使用 chmod +x 让你的脚本可执行

chmod +x myscript.py

然后执行 crontab -e 并添加 0 0 * * 2,5,0/path/to/my/script/myscript.py

每周二、周五、周日为 2、5、0。

关于python - 如何在 bash 中的一周中的特定一天只运行一次 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32672654/

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