Script run by Cron is not writing to file.

kthibault17

New Member
Credits
20
Hello all,

I have a python script that opens a file and writes output to that file from the Raspberry Pi Sense Hat. When I run this script from the terminal it logs its data to the file. When this script is run from a Cron job, the output file gets created but no data is written. Any thoughts would be greatly appreciated.

Thanks!
 


dcbrown73

Well-Known Member
Credits
2,836
This is likely script specific and without seeing the script, it might be more difficult to understand why it's not functioning as expected.

For instance, a cronjob is not $PATH aware as it does not execute a user profile before executing the script. So, if you're calling something within the script (like a subprocess system call) it might not find the system call because it doesn't have the absolute path to it.

Now, I'm not saying that is your issue, but it's hard to know what the core issue with without knowing the details.
 

kthibault17

New Member
Credits
20
My apologies, here are some more details.

This is the script I'm running:

Code:
from sense_hat import SenseHat
import time
import datetime

# Import smtplib for the actual sending function
import smtplib, ssl

#Get the temperature data and write it to the file
def temp_data(sense, wfile):
temp = sense.get_temperature_from_humidity()
temp_F = (temp * 1.8) + 32
timeStamp = datetime.datetime.now()
wfile.write("Time Stamp: %s \n" % timeStamp)
wfile.write("Temperature: %s C \n" % round(temp, 4))
wfile.write("Temperature: %s F \n" % round(temp_F, 4))
#print(datetime.datetime.now())
#print("Temperature: %s C" % round(temp, 4))
#print("Temperature: %s F" % round(temp_F, 4))

#Get the pressure/elevation data and write it to the file
def pressure_data(sense, wfile):
pressure = sense.get_pressure()
alt_in_feet = (1 - ((pressure / 1013.25) ** 0.190284)) * 145366.45
wfile.write("Pressure: %f Millibars \n" % pressure)
wfile.write("Altitude in ft: %f ft \n" % alt_in_feet)
wfile.write("\n")
#print("Pressure: %f Millibars" % pressure)
#print("Altitude in ft: %f ft" % alt_in_feet)
#print("\n")

def main():
#Open a file to write the sensor data to.
wf = open("sensor_data.txt", "a")

sense = SenseHat()
while True:
temp_data(sense, wf)
pressure_data(sense, wf)
#Sleep for 60 seconds
time.sleep(60)

wf.close()
if __name__ == "__main__":
main()
I run it from the terminal as: $ python3 sensor_data.py

I run the cron job as: @reboot python3 /home/pi/Desktop/sensor_data.py

Please let me know if you need any other info. Thanks again!
 

dcbrown73

Well-Known Member
Credits
2,836
I would suggest you add the path to Python3 to your cronjob.
Code:
@reboot /path/to/python3 /home/pi/Desktop/sensor_data.py
As noted in my original post, cronjobs do not have access to profile path environment variable.

You can run the following to get the path to your python3 executable if necessary.
Code:
[[email protected] ~]$ which python3
/usr/bin/python3
 

JasKinasis

Well-Known Member
Credits
7,583
I would suggest you add the path to Python3 to your cronjob.
Code:
@reboot /path/to/python3 /home/pi/Desktop/sensor_data.py
As noted in my original post, cronjobs do not have access to profile path environment variable.

You can run the following to get the path to your python3 executable if necessary.
Code:
[[email protected] ~]$ which python3
/usr/bin/python3
An alternative would be to put a shebang line at the start of the Python script:
Bash:
#!/usr/bin/env python3
The shebang must be the very first line in the file, before the imports! That tells the shell which interpreter to load, in order to run the script. That way you wouldn’t need to specify the path to the interpreter in cron.

Without specifying the path in cron and without a shebang, the shell will assume the script is written in its own language.

So if your default shell is bash, it will run in bash, if it’s zsh, it will run in zsh.

So, assuming your default shell is bash:
Because your script is a Python script, running it as a cron job without somehow specifying that the script requires the Python interpreter will result in the shell running the Python script as a bash script. And then it will just crash out because the Python syntax will cause bash to throw syntax errors, or command unknown type error messages.
So from the cron perspective the file will just run and fail. The file writing in your script is done via code in your Python script.
That’s why nothing went out to your log files - because none of the Python code got executed.

Adding a shebang to your script, or doing as @dcbrown73 has recommended will ensure that the shell runs the script as a Python script and your log files will be written to!
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Staff online


Latest posts

Top