使用cron启动python脚本并输出打印到文件 [英] Start python script with cron and output print to a file
问题描述
我使用cron启动一个python脚本,如下所示:
#Python脚本每隔3分钟调用一次崩溃
* / 3 * * * * /home/ubuntu/python_script.sh&
我的脚本有几个打印调试。我想将这些消息打印到一个文件,例如/home/ubuntu/Logs.txt
我的脚本还检查python调用是否已经运行, t再次启动:
#!/ bin / bash
lines = $(ps -ef | grep python_script。 py | wc -l)
if [$ lines -ne 2];然后
python python_script.py&
fi
如何将我的邮件打印到此文件?我使用的是python 2.7
更新:用以下代码编辑我的代码:
1)将此添加为我的python脚本的第一行:
从__future__ import print_function
2)在上次导入后打开日志文件,然后添加测试打印:
log = open(/ home / ubuntu / Logs.txt,w +)
print ,file = log)
如果我简单地从终端运行它, 。但是如果我用cron安排它,它不会写入文件。
更新2:解决方案与logger命令。在我的shell脚本中,我不得不添加2>& 1(在shell脚本中,不是由ElmoVanKielmo描述的crontab)和| logger&。最终解决方案如下:
#!/ bin / bash
lines = $(ps -ef | grep python_script .py | wc -l)
if [$ lines -ne 2];然后
python python_script.py 2>& 1 |记录器
fi
并将任何打印输出到/ var / log / syslog文件
b p $ p>
* / 3 * * * * /home/ubuntu/my_script.sh>> /home/ubuntu/Logs.txt
它会将stdout重定向到此文件。
您可能还想要有stderr重定向(异常等),因此:
* / 3 * * * * / home / ubuntu / my_script.sh>> /home/ubuntu/Logs.txt 2> /home/ubuntu/errors.txt
或者您可以将它们全部放在一个文件中:
* / 3 * * * * /home/ubuntu/my_script.sh> /home/ubuntu/Logs.txt 2>& 1
I'm starting a python script using cron, like this:
#Python script called every 3 minutes in case script crashes
*/3 * * * * /home/ubuntu/python_script.sh &
And my script has several prints for debugging. I want to print those messages into a file, for example /home/ubuntu/Logs.txt
My script also check if the python call is already running so it won't start it again:
#!/bin/bash
lines=$(ps -ef | grep python_script.py | wc -l)
if [ $lines -ne 2 ]; then
python python_script.py &
fi
How do I print my messages to this file? I'm using python 2.7
Update: Edited my code with the following:
1) Added this as the first line of my python script:
from __future__ import print_function
2) Opened the log file after the last import, and right after that added a test print:
log = open("/home/ubuntu/Logs.txt", "w+")
print("Testing...", file = log)
If I simple run it from the terminal, it prints to the file fine. But if I schedule it with cron it doesn't write to the file.
Update 2: Ended up stumbling on a solution with "logger" command. On my shell script I had to add "2>&1" (in the shell script, not crontab as described by ElmoVanKielmo) and " | logger &". Final solution looks like this:
#!/bin/bash
lines=$(ps -ef | grep python_script.py | wc -l)
if [ $lines -ne 2 ]; then
python python_script.py 2>&1 | logger &
fi
and it outputs any prints to /var/log/syslog file, which is good enough for me.
In your crontab put this:
*/3 * * * * /home/ubuntu/my_script.sh >> /home/ubuntu/Logs.txt
It will redirect stdout to this file.
You might also want to have stderr redirected (exceptions etc.), so:
*/3 * * * * /home/ubuntu/my_script.sh >> /home/ubuntu/Logs.txt 2>> /home/ubuntu/errors.txt
Or you can have them all in one file:
*/3 * * * * /home/ubuntu/my_script.sh >> /home/ubuntu/Logs.txt 2>&1
这篇关于使用cron启动python脚本并输出打印到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!