0

It is my first post here.

I am creating a simple bash script which should run a python command and mail the output. However, every time the script is running via cron, half of the python output is missing. Whereas, if I run the same script manually on shell, everything works as expected.

The bash script is:

#!/usr/local/bin/python3

echo $(/usr/local/bin/python3 /home/tech2/myscript_v3_4-3.py -rs) > /home/tech2/weeklyreportoutput.txt

sleep 180

echo "Data Generated on $(date +%T_%d_%m_%y)" | mail -s "Data for Weekly SYNC $(date +%A_%F_%T)" -a /home/tech2/weeklyreportoutput.txt tech2@example.com

Note: The python script runs max for 10 seconds and output is not more than 100 lines. I have also tried without echo, but in that case python doesn't run with -rs argument.

Any help would be really appreciated.

  • 1
    Welcome to the site. Please edit your question to include examples of how the output should look like (and does, if you run the script by hand), and how it looks when run from cron. Also, it is unusual for a bash script to start with #!/usr/local/bin/python3... – AdminBee Feb 3 '20 at 11:44
  • Thanks for replying guys! If I run it without echo, the python command doesn't take -rs argument. This argument/switch is necessary to get desired reports. I also tried to replacing #!/usr/local/bin/python3 with #!/bin/sh, no changes in the behavior of the script. – Tech99 Feb 3 '20 at 11:50
  • 1
    The way it is now, your bash script will be run with the python interpreter (unless you start it like bash myscript.sh). This is wrong. You should either use #!/bin/sh for a sh script and #!/bin/bash for a bash script. – Panki Feb 3 '20 at 12:49
0

Why do you run the python program, capture the output, then echo the (unquoted) output? Just do

/usr/local/bin/python3 /home/tech2/myscript_v3_4-3.py -rs > /home/tech2/weeklyreportoutput.txt

One of the main problems with your approach is that newlines will replaced by spaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.