【问题标题】:Shell script / Python3 file on crontabcrontab 上的 Shell 脚本/Python3 文件
【发布时间】:2017-07-25 17:06:37
【问题描述】:

我尝试在 crontab 中运行 schedule.sh(shell 脚本)

这是脚本中的代码

#!/bin/sh -x
SHELL=/bin/bash
PATH=/home/peteryph/bin:/home/peteryph/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin  

python3 main_schedule.py
echo "Finish"

在 crontab 终端中(crontab -e)

* * * * * /home/peteryph/Desktop/working/city_google/schedule.sh > /home/peteryph/Desktop/working/city_google/echo2.txt
* * * * * /usr/bin/python3 /home/peteryph/Desktop/working/city_google /schedule.py > /home/peteryph/Desktop/working/city_google/echo.txt

处于 cron 状态

(peteryph) CMD (/home/peteryph/Desktop/working/city_google/schedule.sh > /home/peteryph/Desktop/working/city_google/echo2.txt)
(peteryph) CMD (/usr/bin/python3 /home/peteryph/Desktop/working/city_google /schedule.py > /home/peteryph/Desktop/working/city_google/echo.txt)

在main_schedule中,代码是这样的

(我确实导入了文件。它们很多,所以我没有把它们放在这里。)

print("Please wait.........\n")
infor = update()
infor.clear_previous_value() #update the data in the DB
start_time = time.time()
infor.select_update() #insert the data into DB
print("Running time --%s seonds--" %(time.time() - start_time) + "\n")
sys.exit()

我尝试同时执行 python 文件和脚本,但我只收到来自 python 文件的回显。但是数据没有在 crontab 上的 python 文件和脚本上插入数据库。

但是,当我手动执行脚本时,它可以工作。

(我的文件中确实有非英文字符,这会影响 crontab 吗?)

(或者数据库的路径必须是绝对路径?)

【问题讨论】:

    标签: python-3.x sqlite cron


    【解决方案1】:

    我找到了问题所在。

    首先,当你为 sqlite3 调用数据库时,你必须为你的数据库使用绝对路径。

    其次,确保你的 shell 脚本是可执行的:P

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 2015-05-01
      • 2019-10-18
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多