The following is the Python script that I want to execute every time Raspberry Pi reboots. When I run this Python script directly from the terminal it works. Not only does IP gets stored in the file but also IP is sent to AWS when this script is executed from the terminal.
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import json
import socket
import time
ENDPOINT = "atfvhlb5vayj2-ats.iot.us-east-1.amazonaws.com"
PORT = 8883
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip_address = s.getsockname()[0]
s.close()
return ip_address
# time.sleep(30)
ip_address = get_ip_address()
while(1):
if(ip_address != None):
break
file_path = "/home/pi/Documents/get_ip_py2/ip.txt"
with open(file_path,'w') as file:
file.write(ip_address)
myMQTTClient = AWSIoTMQTTClient(clientID="ip_thing")
myMQTTClient.configureEndpoint(ENDPOINT, PORT)
myMQTTClient.configureCredentials(
CAFilePath = "/home/pi/Downloads/AmazonRootCA1.crt",
KeyPath = "/home/pi/Downloads/ipthingprivate.pem",
CertificatePath = "/home/pi/Downloads/ipthingcertificate.pem")
myMQTTClient.connect()
myMQTTClient.publish(topic="elit/deviceRegister", payload=json.dumps(ip_address),QoS=1)
myMQTTClient.disconnect()
Here is a cronjob that I have created for this:
@reboot /usr/bin/python /home/pi/Documents/get_ip_py2/main.py
This cronjob doesn't execute. This cronjob doesn't even create the file(ip.txt) to store the IP.
I know the path of python and main.py are correct because the following cronjob is executing perfectly.
* * * * * /usr/bin/python /home/pi/Documents/get_ip_py2/main.py
Running the Python script with sudo commands also works:
sudo python main.py
-
You have tagged python2 which has been dead since 2020 and hasn't been in Raspberry Pi OS for years! It is unclear what this is supposed to do but if it is to get IP it is incredibly clumsy. Do you ever put comments in your scripts?Milliways– Milliways2024年01月17日 10:54:56 +00:00Commented Jan 17, 2024 at 10:54
2 Answers 2
A couple of things you should do, and possibly more after you get your log results:
Add redirects to your cron job. This will allow you to capture any
stdout
& (especially)stderr
to a "log file".Add a brief
sleep
before starting your Python script. Whilecron
runs undersystemd
now,cron
&systemd
still assume that all required resources to run your job are available when the script starts.
Try the following entry in your crontab
:
@reboot /usr/bin/sleep 20 && /usr/bin/python /home/pi/Documents/get_ip_py2/main.py >> /home/pi/main_py.log 2>&1
Here's what this does:
Adds 20 seconds of sleep before starting your Python script. This (hopefully) allows sufficient time to get your networked resources established. Nothing magic about 20 seconds... it's just a guess. If 20 secs doesn't work - try 60.
All of the output which would ordinarily go to the terminal (i.e.
stdout
&stderr
) are re-directed to the file/home/pi/main_py.log
.
Based on the info in your question, I feel the sleep
command will cure your issues, but if not, please post the output of /home/pi/main_py.log
as an edit to your question; we'll go from there.
Your script requires network so you should run sudo raspi-config
and set Wait for network at boot
under System settings
.
It usually takes some time to connect to a network at boot.
Even then it may help to include a short delay in your script.