简体   繁体   English

由Cron定期调用的Bash脚本运行时无法执行Python脚本

[英]Python Script Not Executing When Run By a Bash Script Called Periodically by Cron

My Problem is Solved: I provided execute permission to the python scripts, added cron.allow file in the /etc/ directory (with the file containing the only entry "pi", without quotes, which is my username) and provided a full path to the python script. 我的问题已解决:我提供了python脚本的执行权限,在/ etc /目录中添加了cron.allow文件(该文件包含唯一的条目“ pi”,不带引号,这是我的用户名),并提供了完整路径到python脚本。 I am unable to determine which solution made it work, so thanks to glenn jackman and Abhijit Pritam . 我无法确定哪种解决方案可以使它起作用,因此要感谢glenn jackmanAbhijit Pritam

I have two python scripts which are executed by a bash script . 我有两个 bash脚本 执行的 python脚本 Since I want this script to be run every few hours, I have set up a Cron Job . 由于我希望此脚本每隔几个小时运行一次,因此我设置了一个Cron Job The Bash script's ( run_awsscrubber.sh ) job is as follows: Bash脚本的( run_awsscrubber.sh )作业如下:

  1. Set up the environment. 设置环境。
  2. Write the Bash script run time to two log files (one for all the previous runs and one exclusive to this run). 将Bash脚本运行时写入两个日志文件(一个用于所有先前的运行,另一个用于此运行)。
  3. Check using wget whether an IP address is up or not. 使用wget检查IP地址是否正常。
  4. If its up: 如果向上:
  5. Run script imd_aws_parse_rc4.py and capture its output in a variable and write it to the log files. 运行脚本imd_aws_parse_rc4.py并将其输出捕获到变量中,并将其写入日志文件。
  6. Run script gmail_run_alert.py and capture its output in a variable and write it to the log files. 运行脚本gmail_run_alert.py并将其输出捕获到一个变量中,并将其写入日志文件。
  7. Clean up some generated files 清理一些生成的文件
  8. If it's not up: 如果没有启动:
  9. Run script gmail_run_alert.py and capture its output in a variable and write it to the log files. 运行脚本gmail_run_alert.py并将其输出捕获到一个变量中,并将其写入日志文件。
  10. Clean up some generated files 清理一些生成的文件

imd_aws_parse_rc4.py: The code in this script scrubs a website for information using Selenium WebDriver, Firefox, Gecko driver and Beautiful Soup and stores the parsed data as CSV in a file. imd_aws_parse_rc4.py:此脚本中的代码使用Selenium WebDriver,Firefox,Gecko驱动程序和Beautiful Soup清理网站上的信息,并将解析后的数据以CSV格式存储在文件中。

gmail_run_alert.py: The code in this script emails the CSV file (if it exists) and current generated error log to an ID. gmail_run_alert.py:此脚本中的代码通过电子邮件将CSV文件(如果存在)和当前生成的错误日志发送到ID。

I am using a Raspberry Pi 3 running Raspbian Stretch. 我正在使用运行Raspbian Stretch的Raspberry Pi 3。

The bash script when run manually is able to run both the python scripts but when it is run by Cron the python scripts are not executed. bash脚本在手动运行时能够同时运行两个python脚本,但是当它由Cron运行时,则不会执行python脚本。 The Bash script and python scripts are located in the directory ~/CronJobs and their output is stored in the directory ~/CronOutput Bash脚本和python脚本位于〜/ CronJobs目录中,其输出存储在目录〜/ CronOutput中

Following is some code/output which might be useful for finding the solution: 以下是一些代码/输出,可能对找到解决方案有用:

Contents of the Cron Job in /etc/cron.d Directory and The File Permissions /etc/cron.d目录中的Cron作业的内容和文件权限

pi@RPI_IronHide:/etc/cron.d $ ls -al AWSScriptCJ
-rw-r--r-- 1 root root 53 May  1 16:56 AWSScriptCJ
pi@RPI_IronHide:/etc/cron.d $ cat AWSScriptCJ
*/2 * * * * pi /home/pi/CronJobs/run_awsscrubber.sh

File Permissions of Bash Script and the Python Scripts Bash脚本和Python脚本的文件权限

pi@RPI_IronHide:~/CronJobs $ ls -al
total 28
-rw-rw-r--  1 pi pi 2264 May  1 10:41 gmail_run_alert.py
-rw-rw-r--  1 pi pi 7166 May  1 10:50 imd_aws_parse_rc4.py
-rwxr-xr-x  1 pi pi 1812 May  1 17:26 run_awsscrubber.sh

Permissions of CronJobs and CronOutput Directories CronJob和CronOutput目录的权限

pi@RPI_IronHide:~ $ ls -al
total 148
drwxr-xr-x  2 pi   pi    4096 May  1 17:27 CronJobs
drwxr-xr-x  2 pi   pi    4096 May  1 16:55 CronOutput

Single Run Log File Contents 单次运行日志文件内容

pi@RPI_IronHide:~/CronOutput $ cat AWS_Data_Scrub_Latest.log
Tue 1 May 17:56:01 IST 2018
LANG=en_GB.UTF-8
PWD=/home/pi
HOME=/home/pi
SHELL=/bin/bash
SHLVL=1
LOGNAME=pi
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/printenv
Script Begin!
Online

Removing geckodriver.log File
Removing aws_html.html File

Removing Single Run CSV File
Script End Success!

Cron Logs from /var/log/syslog 来自/ var / log / syslog的Cron日志

pi@RPI_IronHide:~ $ grep CRON /var/log/syslog | tail -10
May  1 17:48:01 RPI_IronHide CRON[2555]: (pi) CMD (/home/pi/CronJobs/RunACronTask.sh)
May  1 17:48:01 RPI_IronHide CRON[2556]: (pi) CMD (/home/pi/CronJobs/run_awsscrubber.sh)
May  1 17:50:01 RPI_IronHide CRON[2590]: (pi) CMD (/home/pi/CronJobs/run_awsscrubber.sh)
May  1 17:50:01 RPI_IronHide CRON[2591]: (pi) CMD (/home/pi/CronJobs/RunACronTask.sh)
May  1 17:52:01 RPI_IronHide CRON[2625]: (pi) CMD (/home/pi/CronJobs/RunACronTask.sh)
May  1 17:52:01 RPI_IronHide CRON[2626]: (pi) CMD (/home/pi/CronJobs/run_awsscrubber.sh)
May  1 17:54:01 RPI_IronHide CRON[2675]: (pi) CMD (/home/pi/CronJobs/run_awsscrubber.sh)
May  1 17:54:01 RPI_IronHide CRON[2674]: (pi) CMD (/home/pi/CronJobs/RunACronTask.sh)
May  1 17:56:01 RPI_IronHide CRON[2725]: (pi) CMD (/home/pi/CronJobs/run_awsscrubber.sh)
May  1 17:56:01 RPI_IronHide CRON[2726]: (pi) CMD (/home/pi/CronJobs/RunACronTask.sh)

Bash Script Contents Bash脚本内容

pi@RPI_IronHide:~/CronJobs $ cat run_awsscrubber.sh
#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SHELL=/bin/bash
MAILTO=""

LOG_FILE_N="/home/pi/CronOutput/AWS_Data_Scrub.log"
CURR_LOG_FILE="/home/pi/CronOutput/AWS_Data_Scrub_Latest.log"

echo $(/bin/date) >> $LOG_FILE_N
printenv >> $LOG_FILE_N
echo $(/bin/date) > $CURR_LOG_FILE
printenv >>$CURR_LOG_FILE

echo "Script Begin!" >> $LOG_FILE_N
echo "Script Begin!" >> $CURR_LOG_FILE

/usr/bin/wget -q --tries=10 --timeout=20 --spider XXX.XXX.167.219

if [[ $? -eq 0 ]]; then
    echo "Online" >> $LOG_FILE_N
    echo "Online" >> $CURR_LOG_FILE
    CURRENT_RUN_OP=$(python imd_aws_parse_rc4.py)
    echo $CURRENT_RUN_OP >> $CURR_LOG_FILE
    echo $CURRENT_RUN_OP >> $LOG_FILE_N
    echo "Removing geckodriver.log File" >> $LOG_FILE_N
    echo "Removing geckodriver.log File" >> $CURR_LOG_FILE
    rm /home/pi/CronJob/geckodriver.log
    echo "Removing aws_html.html File" >> $LOG_FILE_N
    echo "Removing aws_html.html File" >> $CURR_LOG_FILE
    rm /home/pi/CronOutput/aws_html.html
    EMAIL_RUN_OP=$(python gmail_run_alert.py)
    echo $EMAIL_RUN_OP >> $CURR_LOG_FILE
    echo $EMAIL_RUN_OP >> $LOG_FILE_N
    echo "Removing Single Run CSV File" >> $LOG_FILE_N
    echo "Removing Single Run CSV File" >> $CURR_LOG_FILE
    rm /home/pi/CronOutput/strip_d_temp.csv
    echo "Script End Success!" >> $CURR_LOG_FILE
    echo "Script End Success!" >> $LOG_FILE_N
    echo -e "\n" >> $LOG_FILE_N
    exit 1
else
    echo "Offline" >> $LOG_FILE_N
    echo "Offline" >> $CURR_LOG_FILE
    EMAIL_RUN_OP=$(python gmail_run_alert.py)
    echo $EMAIL_RUN_OP >> $CURR_LOG_FILE
    echo $EMAIL_RUN_OP >> $LOG_FILE_N
    echo "Script End Unable to Access Website!" >> $CURR_LOG_FILE
    echo "Script End Unable to Access Website!" >> $LOG_FILE_N
    echo -e "\n" >> $LOG_FILE_N
    exit 0
fi

Please provide execute permission to Python script as well. 请同时提供对Python脚本的执行权限。 Also check whether your cron tab account has been added to the group which has the ownership of your shell script or not. 还要检查您的cron tab帐户是否已添加到拥有shell脚本所有权的组中。 If not you need to add that account to the proper group. 如果不是,则需要将该帐户添加到适当的组。 File permission itself it not suffice. 文件权限本身是不够的。 Also check whether cron tab account has full excess to the directory path or not. 还要检查cron tab帐户是否具有完全多余的目录路径。 For example suppose your directory path is /home/pi/CronJobs/ now cron tab account should have permission for cd /home/pi/CronJobs/ . 例如,假设您的目录路径为/home/pi/CronJobs/ cron tab帐户应具有cd /home/pi/CronJobs/权限。

You don't provide a full path to the python script. 您没有提供python脚本的完整路径。 Pick one of 选择其中之一

EMAIL_RUN_OP=$(python /home/pi/CronJobs/gmail_run_alert.py)

or 要么

cd /home/pi/CronJobs
EMAIL_RUN_OP=$(python gmail_run_alert.py)

or 要么

cd "$(dirname "$0")"    # the directory of the current script, without hardcoding
EMAIL_RUN_OP=$(python gmail_run_alert.py)

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

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