简体   繁体   English

在EC2上运行cronjob(Python写入文件)

[英]Running cronjob (Python write file) on EC2

I have a simple Python script on EC2 to write a text to a file: 我在EC2上有一个简单的Python脚本,可将文本写入文件:

with open("/home/ec2-user/python/test.txt", "a") as f:
    f.write("test\n\n")

That python script is here: 该python脚本在这里:

/home/ec2-user/python/write_file.py

When I run that script manually ('python /home/ec2-user/python/write_file.py') - new text is being written to a file ('/home/ec2-user/python/test.txt'). 当我手动运行该脚本时('python /home/ec2-user/python/write_file.py')-新文本正在写入文件('/home/ec2-user/python/test.txt')。

When schedule same script using Cronjob - no data is being added to a file. 使用Cronjob计划相同脚本时-没有数据被添加到文件中。 My Cronjob looks like this: 我的Cronjob看起来像这样:

* * * * * python /home/ec2-user/python/write_file.py

I verify Cronjob is running and suspecting some ENV parameters are not the same during Cronjob execution (or something else is happening). 我确认Cronjob正在运行,并怀疑在执行Cronjob期间某些ENV参数不相同(或正在发生其他情况)。 What could be the case and how to fix it in a simple way? 可能是什么情况,以及如何以简单的方式解决?

Thanks. 谢谢。

it's bette to specify the absolute path of the python interpreter because the command may not be recognized by the system. 最好指定python解释器的绝对路径,因为系统可能无法识别该命令。 Or you can add the shebang line to your script #!pathToPythonInterpreter at the first line of your script 或者,您可以在脚本的第一行将shebang行添加到脚本#!pathToPythonInterpreter

make sure that the user who planning the execution of script have the right to read and write to the file 确保计划执行脚本的用户有权读取和写入文件

make sure that you are not using the system corntab /etc/crontab to planify because then you have to specify the user who will execute the command. 确保您没有使用corntab / etc / crontab系统进行规划,因为这时必须指定将执行命令的用户。

I hope that this will help you 希望对您有帮助

I recently began using Amazon's linux distro on ec2 instances and after trying all kinds of things for cron all I needed was: 我最近开始在ec2实例上使用Amazon的Linux发行版,在尝试了cron的各种功能之后,我需要做的就是:
sudo service crond start
crontab -e
This allowed me to set a cron job as "ec2-user" without specifying the user or env variables or anything. 这使我可以将cron作业设置为“ ec2-user”,而无需指定用户或env变量或其他任何内容。 For example, this single line worked: 例如,此行有效:
0 12 * * * python3 /home/ec2-user/example.py
I also posted this here . 我也把这个贴在这里

On crontab 在crontab上

HOME=/home/ec2-user/
* * * * * python /home/ec2-user/python/write_file.py

alternatively (testing purpose) 或者(测试目的)

* * * * * root /home/ec2-user/python/write_file.py

On your write_file.py add shebang (first line) 在您的write_file.py添加shebang(第一行)

#!/usr/bin/env python

BONUS: (feel free to ignore) my first response debug method for cron on minute tasks: 奖励:(随意忽略)我对微小任务的cron的第一个响应调试方法:

import os
while True:
    print os.popen('pgrep -lf python').read()

executed from shell will pop out all python modules called by cron. 从shell执行将弹出cron调用的所有python模块。 After 60 secs you know whats going on. 60秒后,您知道发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM