Python Forum
Help creating shell scrip for python file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help creating shell scrip for python file
#1
Hi guys,

So Im reading through this git (https://github.com/cubapp/RasPi-EnviropH...ree/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
Reply
#2
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.
Reply
#3
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
Reply
#4
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?
Reply
#5
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?
Reply
#6
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...

Attached Files

Thumbnail(s)
   
Reply
#7
Try /home instead of home perhaps.
Reply
#8
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?
Reply
#9
Try
Output:
chmod +x /home/mkzero/shenviro.sh
to make the file executable.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Launch Python IDLE Shell from terminal Pavel_47 5 1,262 Feb-17-2023, 02:53 PM
Last Post: Pavel_47
  Creating csv files from Excel file azizrasul 40 5,718 Nov-03-2022, 08:33 PM
Last Post: azizrasul
  batch file for running python scipt in Windows shell MaartenRo 2 1,907 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  Creating file with images BobSmoss 1 1,397 Jan-08-2022, 08:46 PM
Last Post: snippsat
  Creating csv file from another file pisike 0 1,611 Nov-24-2020, 02:02 PM
Last Post: pisike
  How to make a Python program run in a dos shell (cmd) Pedroski55 2 2,334 Nov-09-2020, 10:17 AM
Last Post: DeaD_EyE
Bug Python Shell 3.9.0 - Issue with indentation Earis 17 6,664 Oct-31-2020, 07:00 AM
Last Post: Earis
  Creating Conda env from requirments.txt file ErnestTBass 7 15,758 Apr-23-2020, 06:57 PM
Last Post: snippsat
  Python "Terminal" vs "Shell" SectionProperties 2 2,677 Apr-10-2020, 08:36 AM
Last Post: SectionProperties
  Creating a pdf from file DT2000 4 2,341 Apr-03-2020, 07:16 PM
Last Post: DT2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020