Hi, i want a bash one liner to until i control + c it, wait for when the proxy at…
- User
Hi, i want a bash one liner to until i control + c it, wait for when the proxy at socks5://127.0.0.1:9050 isn't working (use curl, get or ping to test it or something else) and do systemctl restart tor and do this over and over until i control c it
- Assistant
Certainly! Here is a bash one-liner that continuously checks if the proxy at
socks5://127.0.0.1:9050is working, and if not, restarts the Tor service usingsystemctl restart tor. This loop continues until you interrupt it withCtrl+C:while true; do curl --socks5-hostname 127.0.0.1:9050 -s --connect-timeout 5 http://www.google.com >/dev/null || systemctl restart tor; sleep 5; doneLet's break down what each part of this one-liner does:
while true; do ... done: This creates an infinite loop, which will run the enclosed commands repeatedly.curl --socks5-hostname 127.0.0.1:9050 -s --connect-timeout 5 http://www.google.com >/dev/null: This command tries to access Google's web page through the SOCKS5 proxy at127.0.0.1:9050.
- User
nevermind, restart every 1 minute
- Assistant
Got it! If you want to check the proxy status every minute and restart the Tor service only if the proxy isn't working, you can update the one-liner as follows:
while true; do curl --socks5-hostname 127.0.0.1:9050 -s --connect-timeout 5 http://www.google.com >/dev/null || systemctl restart tor; sleep 60; doneIn this version:
sleep 60: The script pauses for 60 seconds (1 minute) between checks. This ensures that the Tor service is checked and potentially restarted once every minute.
The rest of the one-liner remains the same, continuously performing the proxy check and restarting the Tor service if the check fails.
- User
dont check for not working, just restart
- Assistant
Understood. If you simply want to restart the Tor service every minute without performing any checks, you can simplify the one-liner:
while true; do systemctl restart tor; sleep 60; doneIn this version:
systemctl restart tor: Restarts the Tor service.sleep 60: Pauses for 60 seconds (1 minute) before restarting the Tor service again.
This way, the script continuously restarts the Tor service every minute.
- User
can u make it so it prints how many seconds is left to restart
- Assistant
Sure! To print a countdown of how many seconds are left until the next restart, you could modify the one-liner to include a loop that counts down the seconds. Here's an updated version:
while true; do systemctl restart tor; for i in {60..1}; do echo -ne "Restarting in $i seconds...\r"; sleep 1; done; doneIn this script:
systemctl restart tor: Restarts the Tor service.for i in {60..1}; do ... done;: A loop that counts down from 60 to 1.echo -ne "Restarting in $i seconds...\r": Prints the countdown message and overwrites the line with\rto update the countdown in place.sleep 1: Pauses for 1 second for each iteration of the countdown loop.
This loop ensures that the script prints the countdown every second until the next service restart.