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

2024-09-30 20:33:11 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,伙计们,我想就我的bash脚本寻求一些帮助。 我在bash脚本中运行2个python脚本,当我手动运行它时,它可以工作,但当我使用cron时,只有.sh文件中的命令不能在.py上工作

请注意,我已经为python3安装了必要的UTIL和软件包

以下是脚本:

#!/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要检查,并且只在.sh文件中显示命令

我的亲信里面有

SHELL=/bin/bash
路径=/sbin:/bin:/usr/bin:/usr/bin
MAILTO=root


Tags: ofthetopyecho脚本bashbin
2条回答

您的路径看起来是错误的:

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不知道特定脚本在服务器中的位置

相关问题 更多 >