简体   繁体   English

为什么Python无法正确设置我的cron作业时间?

[英]Why is Python not setting up my cron job's time properly?

I created a python script which adds a data entry in a mysql database every 1 minute. 我创建了一个python脚本,每1分钟在mysql数据库中添加一个数据条目。 For that, I used cron inside python.Here's the code of script1.py: 为此,我在python中使用了cron。这是script1.py的代码:

import MySQLdb as mdb
from crontab import CronTab

con=mdb.connect('localhost','prabakar','****','timedb');
with con:
    cur=con.cursor()
    cur.execute("DROP TABLE IF EXISTS timer")
    cur.execute("CREATE TABLE timer(Time TIME)")
    cur.execute("INSERT INTO timer(Time) VALUES(CURRENT_TIMESTAMP)")
cron= CronTab(user=True)
job=cron.new(command='/home/praba1110/Delta_sysad_tasks/Task  2/script2.py')
job.minute.every(1)
cron.write()

Where script2.py is, 在script2.py所在的位置,

    import MySQLdb as mdb
    con=mdb.connect('localhost','prabakar','****','timedb');
    with con:
          cur=con.cursor()
          cur.execute("INSERT INTO timer(Time) VALUES(CURRENT_TIMESTAMP)")

When I executed the command 'crontab -e' command in my terminal, it showed an entry of 当我在终端中执行命令“ crontab -e”命令时,它显示了

    * * * * * /home/praba1110/Delta_sysad_tasks/Task  2/script2.py

instead of, 代替,

    */1 * * * * /home/praba1110/Delta_sysad_tasks/Task  2/script2.py

When I made the 2nd script run seperately, it did run and appended the current time in the table.But when trying to execute from script1, it isn't working.The script 2 is not getting executed from script 1. PS: I did give rwx permissions to script2.py 当我使第二个脚本单独运行时,它确实运行并将当前时间附加到表中。但是,当尝试从script1执行时,它不起作用。脚本2无法从脚本1中执行。PS:我做了向rwx授予script2.py的权限

Can anyone tell why script1.py isn't working or what the problem is? 谁能说出script1.py为什么不起作用或问题出在哪里?


Solved 解决了

It works if we give 如果我们给
job=cron.new(command='/user/bin/python2 "/home/praba1110/Delta_sy sad_tasks/Task 2/script2.py" ') job = cron.new(command ='/ user / bin / python2“ / home / praba1110 / Delta_sy sad_tasks / Task 2 / script2.py”')

* * * * * /home/praba1110/Delta_sysad_tasks/Task  2/script2.py

is equivalent to 相当于

*/1 * * * * /home/praba1110/Delta_sysad_tasks/Task  2/script2.py

Neither of these crontab entries will work however, since the shell will try to execute 但是,这两个crontab条目都不起作用,因为外壳程序将尝试执行

/home/praba1110/Delta_sysad_tasks/Task

and pass to it the argument 2/script2.py . 并将参数2/script2.py传递给它。 Instead, you'll need to protect the spaces in the directory path by enclosing the command in quotes: 相反,您需要通过将命令括在引号中来保护目录路径中的空格:

job=cron.new(command='/path/to/python "/home/praba1110/Delta_sysad_tasks/Task  2/script2.py"')

so that the crontab entry becomes 使crontab条目变为

* * * * * /path/to/python "/home/praba1110/Delta_sysad_tasks/Task  2/script2.py"

Note that handling spaces correctly is possible by often an annoyance. 请注意,经常会感到烦恼,可能会正确处理空间。 You might want to avoid spaces in paths to make your life easier. 您可能希望避免在路径上留出空间以使您的生活更轻松。

Per Moo's suggestion, I've also added an explicit absolute path to the python executable since by default cron jobs do not see the same environment variables (such as PATH) as you would see when logged on. 根据Moo的建议,我还为python可执行文件添加了一条明确的绝对路径,因为默认情况下,cron作业不会看到与登录时相同的环境变量(例如PATH)。

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

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