在cron中引用ini文件失败 [英] Referencing ini file fails in cron

查看:80
本文介绍了在cron中引用ini文件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询数据库的python脚本。我从终端运行 python3 myscript.py

I have a python script that queries a database. I run it from the terminal with python3 myscript.py

我在其中添加了一个cron任务crontab文件
* / 30 9-17 * * 1-5 python3 / path / to / my / python / script\目录\ space / myscript.py

I've added a cron task for it in my crontab file */30 9-17 * * 1-5 python3 /path/to/my/python/script\ directory\ space/myscript.py

该脚本在同一目录中导入一个函数,该函数解析位于同一目录中database.ini中的数据库的登录信息。 database.ini是:

The script imports a function in the same directory that parses login info for a database located in database.ini in the same directory. The database.ini is:

[postgresql]
host=my-db-host-1-link.11.thedatabase.com
database=dbname
user=username
password=password
port=10898

但当前cron输出到我的邮件文件夹中的文件:

But currently cron outputs to the file in my mail folder:

Section postgresql not found in the database.ini file

中找到的

该部分显然存在于database.ini文件中,所以我在这里丢失了吗?

The section is clearly present in the database.ini file, so what am I missing here?

推荐答案

而不是在当前目录中运行 python3 myscript.py,请尝试运行它来自其他目录(例如主目录)。

Instead of running "python3 myscript.py" in the directory where it is present, try running it from some other directory (like home directory). Most likely you will see the same issue.

请注意,cron的current-working-directory在不同的系统上是不同的。因此,最安全的方法是显式切换到脚本所在的目录并在该目录中运行命令:

Note that cron's current-working-directory is different on different systems. So, the safest method is to explicitly switch to the directory where your script is and run the command there:

cd /path/to/my/python/script\ directory\ space/ && python3 myscript.py

这篇关于在cron中引用ini文件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆