0

I have a shell script which runs python then attaches a pdf to an email via mutt.

Shell script:

cd ./pod_reports/script/
whereareyou=`pwd`
OUT=`python3 dummy.py`
cd ..
filename=$(ls *.pdf -1t | head -1)
strt="echo Did we get python? "
mailmutt="| mutt -s 'Subject Line' -a $filename -- [email protected]"
stmt="$strt$whereareyou$OUT$mailmutt"
echo $stmt
eval $stmt

Python Script, dummy.py

import sys
OUT = sys.stdout.write(" I did python!! ")
sys.exit(0)

When the shell script is run directly from the command line, I get the expected pwd via 'wherareyou' and the 'I did python!!' statement in the email body, along with the expected attachment. When the script scheduled via crontab, I get the same expected 'whereareyou' and attachment, but I do not get the message from python. My understanding from this is that python is not getting run at all when the shell is executed by crontab.

I don't understand why. Is this somehow expected behavior from crontab? If so, how can a series of scripts be scheduled if not within a shell?

2
  • think you need absolute path to dummy.py
    – user986805
    Feb 1, 2020 at 8:27
  • I have done it both ways (using both relative and absolutes) in the course of troubleshooting (and that's the purpose of checking where I am with the 'whereareuou' pwd command in this iteration). I've found other questions now where the issue is the crontab environment doesn't have the same programs set to it as the command line (e.g. python3 may not be a command it recognizes). I'm looking into ways to make that happen and will update.
    – AlexJ
    Feb 1, 2020 at 14:06

1 Answer 1

0

Answer: crontab may have different program paths than your command line.

As it so happens, in our system the 'mutt' (email) program was stored in /usr/bin/ and this path was known by whatever mechanism runs crontab and so it was successfully able to send emails. However, many programs I wanted to run in this shell, including python3, were located in a different directory: /usr/local/bin/. Programs in this local folder were not known by crontab (indeed, even running which python3 from within a shell scheduled by crontab would return nothing). This is despite both mutt and python3 working when I run a shell from my command line.

The solution is to run the programs of interest (in my case python3) from their absolute paths:

cd ./pod_reports/script/
whereareyou=`pwd`
OUT=`/usr/local/bin/python3 dummy.py`
cd ..
filename=$(ls *.pdf -1t | head -1)
strt="echo Did we get python? "
mailmutt="| mutt -s 'Subject Line' -a $filename -- [email protected]"
stmt="$strt$whereareyou$OUT$mailmutt"
echo $stmt
eval $stmt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .