繁体   English   中英

在 Bash 中运行 Python 脚本在 Crontab 中不工作

[英]Running Python Script Inside Bash Not Working When In Crontab

大家好,我想就我的 bash 脚本寻求一些帮助。 我在我的 bash 脚本中运行 2 个 python 脚本,当我手动运行它时它正在工作,但是当我使用 cron 时,只有 .sh 文件中的命令不起作用 on.py

请注意,我已经为 python3 安装了必要的工具和包。

这是脚本:

#!/usr/bin/env bash

# list.tmp path directory
fileLoc="/home/ec2-user/PushNotification/Incoming34days/list34days.tmp"
# URL to POST request
refLink='http link'
# Title of Push Notification
title='34th day: Grace Period is about to end'
# curl type
type='Notification'
# curl action_type
actionType='NotificationActivity'
# Get the current date and time
now=$(date '+%b %d %Y %H:%M:%S')
# Message to the user
body="Subscribe to the Philippine Mobile Number plan now to continue receiving calls and texts and sending text messages to the Philippines."
# Logs location
logsLoc="/home/ec2-user/PushNotification/Incoming34days/logs.tmp"
# current number
currentNumLoc="/home/ec2-user/PushNotification/Incoming34days/currentNum.tmp"

echo "[$now] Sending notifications to mobile numbers advising today is the last day of grace period..." > $logsLoc
# Python file to SELECT all id who has 34 days counter
python3 select34days.py
# psql -d $database -t -c "SELECT id FROM svn WHERE current_date - expiry_date::DATE = 4" psql must be setup using .pgpass for postgresql authentication, please indicated database
# name and query list directory. Deleting the last line from list.txt

# This is to read the textfile list.txt line per line
while IFS='' read -r list;
# for list in `cat list.txt`;
do
# curl POST request
        response=$(curl --location --request POST $refLink \
                --header 'Authorization: Basic YXBwdm5vdXNlcjphcHB2bm9wYXNz' \
                --header 'Content-Type: application/json' \
                --data-raw '{
                "title":"'"$title"'",
                "body":"'"$body"'",
                "min" :[{"mobileNumber" : "'"$list"'"}],
                "type" : "'"$type"'",
                "action_type" : "'"$actionType"'"}')
# Echo mobile number
        echo "[$now] Mobile Number: $list" >> $logsLoc
# Echo response from curl
        echo "Response: '$response'"
        echo "[$now] Result: '$response'" >> $logsLoc
# Update the current number of the list
        echo $list > $currentNumLoc
        echo "[$now] Updating $list status into EXPIRED" >> $logsLoc
# Updating status into EXPIRED
        python3 updateQuery34.py
done < "$fileLoc"

# end of script

select34days.py 和 updateQuery34.py 没有运行。

我有一个 log.tmp 来检查这种情况,并且只在 my.sh 文件中显示命令

在我的 cron 里面是

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/bin:/usr/bin
MAILTO=root

您的 PATH 看起来不对:

PATH=/sbin:/bin:/usr/bin:/usr/bin

这包括/usr/bin两次,这是不必要的,但暗示应该有其他东西。

根据您的安装方式,python 可能位于/usr/bin//usr/local/bin甚至位于/opt的某个位置。

在命令行中,您可以使用以下命令找到 python 的目录:

dirname $(which python3)

需要将此目录添加到您的 crontab 中的路径中。

每次使用 bash 脚本并将其添加到 cron 作业时,只需使用脚本名称(例如 /etc/test.sh)声明特定目录,因为 cron 不知道服务器中的特定脚本在哪里。

暂无
暂无

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

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