简体   繁体   English

运行 Shell 脚本的 Cron 作业运行 Python 不工作

[英]Cron Job Running Shell Script to Run Python Not Working

As written in the title, I am having some problem with my cron job script not executing.正如标题中所写,我的 cron 作业脚本未执行时遇到了一些问题。 I am using CentOS 7.我正在使用 CentOS 7。

My crontab -e looks like this:我的 crontab -e 看起来像这样:

30 0 * * * /opt/abc/efg/cron_jobs.sh >> /opt/abc/logs/cron_jobs.log

My cron_jobs.sh looks like this:我的 cron_jobs.sh 看起来像这样:

#!/bin/bash

#keep this script in efg folder
#run this daily through crontab -e
#45 0 * * * /opt/abc/efg/cron_job.sh

cd "$(dirname "$0")"
export PYTHONPATH=$PYTHONPATH:`pwd`

#some daily jobs script for abc
date

#send email to users whose keys will expire 7 days later
/usr/local/bin/python2.7 scripts/send_expiration_reminder.py -d 7
#send email to key owners whos keys will expire
/usr/local/bin/python2.7 scripts/send_expiration_reminder.py -d -1

# review user follow status daily task
# Need to use venv due to some library dependencies
/opt/abc/virtualenv/bin/python2.7 scripts/review_user_status.py

So, what I've found is that the log for the cron jobs in /var/logs/cron states that the cron ran at 0:30 am accordingly.因此,我发现 /var/logs/cron 中的 cron 作业的日志表明 cron 相应地在上午 0:30 运行。

Strangely, I find that /opt/abc/logs/cron_jobs.log empty, and the scripts does not seem to run at all.奇怪的是,我发现 /opt/abc/logs/cron_jobs.log 是空的,并且脚本似乎根本没有运行。 It used to output some log before I re-inputted the crontab (to re-install the cron jobs), and replaced cron_jobs.sh, so I think the problem might have arose from those actions.在我重新输入 crontab(重新安装 cron 作业)并替换 cron_jobs.sh 之前,它曾经使用 output 一些日志,所以我认为问题可能来自这些操作。

And also, I would like to know if there are any ways to log the error from executing a python script.而且,我想知道是否有任何方法可以记录执行 python 脚本的错误。 I have been trying to run /opt/abc/virtualenv/bin/python2.7 scripts/review_user_status.py but it never seem to work as intended (does not run the main function at all), and there is no log output whatsoever.我一直在尝试运行 /opt/abc/virtualenv/bin/python2.7 scripts/review_user_status.py 但它似乎从未按预期工作(根本不运行主 function),并且没有任何日志 output。

I tried to run this on a different machine and it works properly, so I am not sure what is wrong with the cron job.我试图在另一台机器上运行它并且它工作正常,所以我不确定 cron 作业有什么问题。

Here is a snippet of the log I got from /var/log/cron to show that the cron called the job:这是我从 /var/log/cron 获得的日志片段,显示 cron 调用了该作业:

Mar 22 18:32:01 web41 CROND[20252]: (root) CMD (/opt/abc/efg/cron_jobs.sh >> /opt/abc/logs/cron_jobs.log)

There are a few areas to check if you haven't performed these already,如果您还没有执行过这些,有几个地方需要检查,

  1. if your executable permissions set on the script, chmod +x <python file> in addition permissions for the user to access the directories.如果您在脚本上设置了可执行权限,则chmod +x <python file>还为用户提供访问目录的权限。

  2. Run the script manually to test the script works from beginning to end, as the user who will be running the script, will be more realistic.手动运行脚本以测试脚本从头到尾是否有效,因为运行脚本的用户会更加真实。

  3. You can test your crontab schedule by temporarily setting every minute for testing, unlike Windows where you can right, click and click Run.您可以通过临时设置每分钟进行测试来测试您的 crontab 计划,这与 Windows 不同,您可以右键单击并单击运行。

First, thank you all for the suggestions and heads up.首先,感谢大家的建议和提醒。 I found out that what was ruining my script is the existence of /r in the line break.我发现破坏我的脚本的是换行符中/r的存在。 Apparently, Linux in general does not accept /r and only accepts /n .显然, Linux 通常不接受/r并且只接受/n

It is because I ftp my files to the machine where the script breaks.这是因为我将我的文件 ftp 放到了脚本中断的机器上。 On the other hand, it works fine on another machine because I used git pull instead of ftp.另一方面,它在另一台机器上工作正常,因为我使用git pull而不是 ftp。

Hope that this info will also be a helpful to others!希望这些信息也对其他人有所帮助!

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

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