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(("", 80))
    ip_address = s.getsockname()[0]
    return ip_address

# time.sleep(30)

ip_address = get_ip_address()

    if(ip_address != None):

file_path = "/home/pi/Documents/get_ip_py2/ip.txt"
with open(file_path,'w') as file:

myMQTTClient = AWSIoTMQTTClient(clientID="ip_thing")

myMQTTClient.configureEndpoint(ENDPOINT, PORT)

CAFilePath = "/home/pi/Downloads/AmazonRootCA1.crt",
KeyPath = "/home/pi/Downloads/ipthingprivate.pem",
CertificatePath = "/home/pi/Downloads/ipthingcertificate.pem")

myMQTTClient.publish(topic="elit/deviceRegister", payload=json.dumps(ip_address),QoS=1)


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
    Jan 17 at 10:54

2 Answers 2


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.


A couple of things you should do, and possibly more after you get your log results:

  1. Add redirects to your cron job. This will allow you to capture any stdout & (especially) stderr to a "log file".

  2. Add a brief sleep before starting your Python script. While cron runs under systemd 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:

  1. 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.

  2. 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 Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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