How to create a script to check for internet connection?

5,602

Add a cron job for root to run the following script:

#!/bin/bash

if ! [ "$(ping -c 1 google.com)" ]; then
    service network-manager restart
fi

Don't forget to make it executable:

chmod +x /path/to/script

To add a cron job for root, use the following command:

sudo crontab -e

And your cron entry from the crontab file should look like:

0/15 * * * * /path/to/script
Share:
5,602

Related videos on Youtube

Mauricio Giraldo
Author by

Mauricio Giraldo

Updated on September 18, 2022

Comments

  • Mauricio Giraldo
    Mauricio Giraldo over 1 year

    I'm new to programming in linux. I started bitcoin mining using linux mint xfce 15. I'm really good following directions, just don't know how to start. I need to create a script that can run every 15 minutes to check if there is a connection to the interet if there is no connection, restart wifi connection. My asus eee b202 is losing internet conection some how, eventhough wifi is connected. So to fix it I disconnect from wifi and reconnect again. With the script added as to a cron job, I can avoid doing that. Thanks before hand.

    • chili555
      chili555 over 10 years
      Have you tried to troubleshoot the wireless?
  • CivMeierFan
    CivMeierFan over 6 years
    I have to use if ! ping -c 1 google.com; then, and a crontab entry with */15 not 0/15 to make this work.