0

Hi guys I would like to ask for some help with my bash script. I am running 2 python script inside my bash script and it is working when I'm running it manually but when I'm using cron only the commands in the .sh file is working not on .py

Please take note that I already install necessary utils and packages for python3.

This is the script:

#!/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

The select34days.py and updateQuery34.py is not running.

I have a log.tmp to check regarding this situation and only displaying commands inside my .sh file

Inside my cron are

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

10
  • 1
    is it finding the python script files? try including the full path to the scripts. Commented Aug 7, 2020 at 3:32
  • I see you've got a point let me try that thanks Commented Aug 7, 2020 at 3:34
  • Still not working bro @ewong Commented Aug 7, 2020 at 3:44
  • You could also cd to the directory with your python files in your sh script. Commented Aug 7, 2020 at 3:45
  • Where is python3? is it finding the python binary? Commented Aug 7, 2020 at 4:02

2 Answers 2

0

Your PATH looks wrong:

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

This includes /usr/bin twice which isn't necessary, but hints that something else should have been there.

Depending on how you've installed it, python might be in /usr/bin/ or /usr/local/bin or even somewhere in /opt.

At the commandline you can find python's directory using:

dirname $(which python3)

This directory needs to be added to your path in your crontab.

Sign up to request clarification or add additional context in comments.

Comments

0

Just declare the specific directory with script name (eg. /etc/test.sh)every time you are using bash scripts and adding it to a cron job since the cron doesn't know where is the specific script within the the server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.