我试图在基于Linux的OS中定期运行一些Python脚本,经过一些快速研究,我发现 crontab 是一种经典的方法。我是该命令的新手,所以我确保牢记常见的现有为此,(谨慎地)我首先决定使用一个非常简单的Python代码, myScript.py
:
#!/usr/bin/python3
print("If you see this, your periodic code ran OK!")
'cron table'() crontab -l </code>)文件看起来如下,该文件被认为每分钟都可以运行 myScript.py
(我想快速测试):
* * * * * /usr/bin/python3 /home/my_user_folder/Desktop/test/myscript.py
秒传递,脚本到达了第一个经过的第一个经过分钟...什么也没发生。为了“解决它”,我尝试了几件事,但是(对我来说)我意识到,大多数(如果不是全部)教程和帖子,用于将消息存储在 .txt 或类似文件。 我做了一些事情(经过几个小时,试验,没有成功)。
#!/usr/bin/python3
# NOTES:
# 1. This code is the 'final version' after several trials
# 2. Remember that I was aiming to automate anything, just
# to get familiar with `crontab`; in this case, storing
# the current time to some .txt file was enough.
import time
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
with open('/home/my_user_folder/Desktop/test/readme.txt', 'a') as f:
f.write(current_time)
f.write('\n')
我通过修改 myScript.py
对: ...和进行修改, 我感到有些愚蠢,因为我必须意识到我的初始实现(在代码,环境设置,权限等方面)确实是正确的,但是使用Python命令使用Python命令 print 使用 crontab
'不起作用的“测试”重复任务' ...
为什么?
I was trying to run some python script periodically in a linux-based OS, and after some quick research, I found that crontab is a classic approach for this. I was a newbie to that command, so I made sure to keep in mind the common existing recommendations for it, and (cautiously) I decided to firstly go with a very simple python code, myscript.py
:
#!/usr/bin/python3
print("If you see this, your periodic code ran OK!")
The 'cron table' (crontab -l
) file looked as follow, which was suppossed to run myscript.py
every minute (I wanted to quick-test it):
* * * * * /usr/bin/python3 /home/my_user_folder/Desktop/test/myscript.py
The seconds passed, the script reached its first elapsed minute... and nothing happened. To "solve it", I tried several things, but eerily (to me) I realized that most (if not all) tutorials and posts, used to store messages in .txt or similar files. I did something alike (after some hours, trials and no success), by modifying myscript.py
to:
#!/usr/bin/python3
# NOTES:
# 1. This code is the 'final version' after several trials
# 2. Remember that I was aiming to automate anything, just
# to get familiar with `crontab`; in this case, storing
# the current time to some .txt file was enough.
import time
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
with open('/home/my_user_folder/Desktop/test/readme.txt', 'a') as f:
f.write(current_time)
f.write('\n')
...and it worked. I felt a bit silly, because I got to realize that my initial implementation (in regards of code, environmental settings, permissions, etc.) was indeed correct from the beginning, and yet using the Python command print
to 'test' recurrent tasks with crontab
'did not work'...
Why?
发布评论
评论(1)
从crontab执行的命令的标准输出 已发送到壳的标准输出。 (这样做对它没有多大意义;在登录后,Cron作业将继续运行。)
通常
crontab
将(尝试)将输出发送给您。这是可配置的。Man 5 Crontab
有关更多信息。您可以在Crontab条目本身中重定向输出,而不是修改Python脚本以重定向其输出:
The standard output of a command executed from crontab is not sent to the standard output of your shell. (It wouldn't make much sense for it to do so; a cron job will continue to run after you've logged out.)
Typically
crontab
will (attempt to) email the output to you. This is configurable.man 5 crontab
for more information.Rather than modifying your Python script to redirect its own output, you can redirect the output in the crontab entry itself: