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.
- Converted code to service
- Disabled device sleep/suspend
- Enabled networking service
- 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