(Apr-10-2022, 08:43 PM)Skaperen Wrote: exiting this script will then reset the network so the pings work again. the purpose of the script is to quickly detect when a remote host can no longer be reached. the idea is N missing ping replies means it is down. then the invoking script resets it and does this, again.In your other Thread talk about ping,and icmplib has a
async_ping
.Would think that this work fine for this task.
To write some more code and make a fake server error(on our server) after 60-sec,then wait 10-sec and do a sever/ping restart.
Run also schedule threaded and async_ping(the name give it way).
from icmplib import async_ping import asyncio import time, os import schedule import threading def server_down(): print('Server is down restart after 10-sec') job = schedule.every(10).seconds.do(run_threaded, check_run1) async def is_alive(address, count): host = await async_ping(address, count, interval=0.2, timeout=4) if host.packets_received == 4: print(f'{host.address} is down!') server_down() else: print(f'{host.address} is up!') def check_run(): return asyncio.run(is_alive('python-forum.io', count=4)) def check_run1(): return asyncio.run(is_alive('python-forum.io', count=0)) def run_threaded(job_func): job_thread = threading.Thread(target=job_func) job_thread.start() if __name__ == '__main__': check_run() schedule.every(60).seconds.do(run_threaded, check_run) while True: schedule.run_pending() time.sleep(1)
Output:104.21.27.41 is down!
Server is down restart after 10-sec
104.21.27.41 is up!
104.21.27.41 is up!
104.21.27.41 is up!
104.21.27.41 is up!
104.21.27.41 is up!
104.21.27.41 is up!
104.21.27.41 is down!
Server is down restart after 10-sec
104.21.27.41 is up!
104.21.27.41 is up!
104.21.27.41 is up!
104.21.27.41 is up!
Quote:maybe, if i get the process ID number, i could kill the process.If want a example of this look this post or post.
psutil is great tool for this.
And of course don't want to check contiguous that use Cpu time,so then use it a schedule way.