Python Forum
Help creating shell scrip for python file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help creating shell scrip for python file (/thread-40746.html)

Pages: 1 2


Help creating shell scrip for python file - marciokoko - Sep-16-2023

Hi guys,

So Im reading through this git (https://github.com/cubapp/RasPi-EnviropHAT-ThinkSpeak/tree/master) and the author writes:

Quote:I call the Python from a shell script via crontab:
$ crontab -l
*/2 * * * * /path/to/update-ts-enviro.sh

$ cat /path/to/update-ts-enviro.sh
#!/bin/bash

/usr/bin/python3 /path/to/print-update-thingspeak-enviro.py >> /logging/path/logs/enviro.csv

Now, ive got the python file created. Then from what i understand, he creates a crontab file and adds this line:

*/2 * * * * /path/to/update-ts-enviro.sh
which calls that .sh file. So i created an empty file called shenviro.sh.

The next 3 lines confuse me. Are these lines in the .sh file:

cat /path/to/update-ts-enviro.sh
#!/bin/bash
/usr/bin/python3 /path/to/print-update-thingspeak-enviro.py >> /logging/path/logs/enviro.csv
Thanks
Mars


RE: Help creating shell scrip for python file - deanhystad - Sep-16-2023

I don't think this line was in script. I think this was the command used to print the script file (update-ts-enviro.sh) to stdout. The lines that follow are in the script file.
Output:
cat /path/to/update-ts-enviro.sh
This is the first line in the script. It is called the "shebang" It tells linux to run the following lines using the bash shell.
Output:
#!/bin/bash
This line is run inside the bash shell. It runs a python program named print-update-thingspeak-enviro.py. Output is redirected to a file named enviro.csv
Output:
/usr/bin/python3 /path/to/print-update-thingspeak-enviro.py >> /logging/path/logs/enviro.csv
All that path/to/ stuff is a standing for the full path to the file that follows.


RE: Help creating shell scrip for python file - marciokoko - Sep-16-2023

Ok well so here is what i have then:

crontab:
*/2 * * * * /shenviro.sh echo "hi there"
shenviro.sh
#!/bin/bash

/usr/bin/python3 /envirophat2.py >> /envirolog.csv
envirophat2.py
#!/usr/local/bin/python3
from envirophat import light, motion, weather, leds
import thingspeak
import time
from datetime import datetime
....plus all the other python code...
1. But upon reboot I dont get the data posted to thingspeak (which does get posted if i run the python file manually using python envirophat2.py).

2. I dont see the hi there

3. I dont get the envirolog.csv file created either


RE: Help creating shell scrip for python file - deanhystad - Sep-16-2023

You need to use full paths here:
Output:
/usr/bin/python3 /envirophat2.py >> /envirolog.csv
Are both those files in /?

All paths in envirphat2.py must also be complete paths, including imports that are not in the python path.

I would not expect you to see "hi there". I don't know where chrontab would echo something. To a log? Maybe it sends your an email?


RE: Help creating shell scrip for python file - marciokoko - Sep-16-2023

envirophat2.py is in my user home directory. I havent created the log file envirolog.csv, so ill do that now.

So my shenviro.sh file is now:

Quote:/usr/bin/python3 /envirophat2.py >> /envirolog.csv

And i checked the path and indeed python3 is at /usr/bin/

envirolog.csv, shenviro.sh and envirophat2.py are all in the home directory:

mkzero@raspberrypi:~ $ ls
envirolog.csv   envirophat2.py         is_example.py  Pimoroni
envirophat1.py  err_missed_events.txt  logrc.txt      shenviro.sh
mkzero@raspberrypi:~ $ nano envirolog.csv
mkzero@raspberrypi:~ $
Are you saying I would need to write out

Quote:/usr/bin/python3 /home/envirophat2.py >> /envirolog.csv
Because I just did that and rebooted and I still dont get any data logs or error logs.

How can I tell if my crontab line is working, since that would be the root of the issue I think? Thats why i added the echo bit.

Mars

(Sep-16-2023, 05:48 PM)deanhystad Wrote: You need to use full paths here:
Output:
/usr/bin/python3 /envirophat2.py >> /envirolog.csv
Are both those files in /?

All paths in envirphat2.py must also be complete paths, including imports that are not in the python path.

I would not expect you to see "hi there". I don't know where chrontab would echo something. To a log? Maybe it sends your an email?



RE: Help creating shell scrip for python file - marciokoko - Sep-16-2023

I also fixed the path in the crontab -e as well to:

*/2 * * * * home/mkzero/shenviro.sh > /home/mkzero/cronlog.log 2>&1
and i get this still:

/bin/sh: 1: home/mkzero/shenviro.sh: not found
Ive attached a screenshot of my home directory...


RE: Help creating shell scrip for python file - Gribouillis - Sep-16-2023

Try /home instead of home perhaps.


RE: Help creating shell scrip for python file - marciokoko - Sep-16-2023

Thanks,

I fixed one of the paths, I was actually missing the "/" before "home" on crontab. So now I get this error in the cronlog.log:

/bin/sh: 1: /home/mkzero/shenviro.sh: Permission denied
How do i fix that?


RE: Help creating shell scrip for python file - Gribouillis - Sep-16-2023

Try
Output:
chmod +x /home/mkzero/shenviro.sh
to make the file executable.


RE: Help creating shell scrip for python file - marciokoko - Sep-16-2023

Got it...plus the csv file was giving me permission denied error as well, so i changed it to envirolog.log instead and it works!

Thanks so much!


(Sep-16-2023, 08:06 PM)Gribouillis Wrote: Try
Output:
chmod +x /home/mkzero/shenviro.sh
to make the file executable.