Score:1

The LAN device IP is not accessible when a SSH session is not running

jp flag

Hi there I have a python script which reads data from different IP addresses in LAN, The script runs in a Edge Device with Ubuntu 20.04 LTS focal installed every 5 min using a CRON job. The problem is very weird, when I SSH into the device, the script simply works but when i close the SSH session, The cron starts the scripts but IP addresses does not respond or not pingable.

What I tried is following but none of the approach worked for me.

  1. Converted code to service
  2. Disabled device sleep/suspend
  3. Enabled networking service
  4. Tried to enable autologin assuming it let the session opened

Looking forward for a solution of this problem, The python script which is executed by the CRON job is following

import subprocess

def ping_ip(ip_address):
    try:
        result = subprocess.run(['ping', '-c', '1', ip_address], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=5)
        if result.returncode == 0:
            return True, result.stdout
        else:
            return False, result.stderr
    except subprocess.TimeoutExpired:
        return False, "Timed out"

def main():
    ip_addresses = ["192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.249"]

    for ip in ip_addresses:
        is_responding, response = ping_ip(ip)
        if is_responding:
            print(f"{ip}\tResponding")
        else:
            print(f"{ip}\t Not Responding")

if __name__ == "__main__":
    main()

The CRON job itself that runs this script every 5 min is following

*/5 * * * * /usr/bin/python3 /home/nvidia/our-script/pinger.py > /home/nvidia/our-script/lastrun.txt

I just discovered same script is working flawlessly without any user login required on device boot on Ubuntu 18.04

Raffa avatar
jp flag
How do you know the IPs are not ping-able? ... Please add any log entries that led you to believe so if any ... Also, showing us the code in your python script might help us understand more ... This seems like either your code needs an active login session to run or your network setup is not optimal as it's not unusual for network equipment to forward requests from/to misconfigured machines momentarily upon a successful connection e.g. SSH and drop them upon disconnecting.
Zain Ul Abidin avatar
jp flag
@Raffa i added the code and cron job which i am using for proof of concept
Raffa avatar
jp flag
I can't see an issue with your script nor with your crntab line ... Now this is leaning more towards a networking issue ... I would add network relating logging and see what it returns ... You can change your crontab line to something like `*/5 * * * * { /usr/bin/python3 /home/nvidia/our-script/pinger.py; /bin/ip route; /bin/ip a; /bin/date; } >> /home/nvidia/our-script/lastrun.txt 2>&1` ... Let it run while logged in via SSH then log out and give it time to run then inspect the log file `lastrun.txt` .
Zain Ul Abidin avatar
jp flag
I will definitely give it a try
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.