LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how can i run/check my script every time in linux machine (https://www.linuxquestions.org/questions/linux-newbie-8/how-can-i-run-check-my-script-every-time-in-linux-machine-4175466020/)

selvavinayak88 06-14-2013 11:26 AM

how can i run/check my script every time in linux machine
 
Hi all,

i have a script that checks whether our internet is UP || DOWN. I need to run/check this script every time

set host1=www.google.com
echo $(((ping -w5 -c3 $host1) > /dev/null 2>&1) && echo "up" || (echo "down" && exit 1))


Note: If my internet connection is down machine is automatically send mail...
for this reason I need check/run this code every time

Any body have knowledge please share with me...

Thanks,
Selvavinayakam.na

sag47 06-14-2013 11:31 AM

Quote:

Originally Posted by selvavinayak88 (Post 4971851)
Hi all,

i have a script that checks whether our internet is UP || DOWN. I need to run/check this script every time

set host1=www.google.com
echo $(((ping -w5 -c3 $host1) > /dev/null 2>&1) && echo "up" || (echo "down" && exit 1))


Note: If my internet connection is down machine is automatically send mail...
for this reason I need check/run this code every time

Any body have knowledge please share with me...

Thanks,
Selvavinayakam.na

Unless you have an alternate internet connection your mail will go undelivered because your internet is down.

That being said assuming you have alternate means of communication for your system, you can run your script regularly using cron (see man pages cron(8), crontab(1), crontab(5)).

I actually recommend you running a more thorough monitoring solution such as Icinga.

SAM

karim.ouda 06-14-2013 11:35 AM

so do you want that script should be executed if internet is down or it should be executed continuously.
if you want it should run in regular interval,use crontab and define your time interval so it will run according to that.

TB0ne 06-14-2013 11:57 AM

Quote:

Originally Posted by selvavinayak88 (Post 4971851)
Hi all,
i have a script that checks whether our internet is UP || DOWN. I need to run/check this script every time

set host1=www.google.com
echo $(((ping -w5 -c3 $host1) > /dev/null 2>&1) && echo "up" || (echo "down" && exit 1))


Note: If my internet connection is down machine is automatically send mail...for this reason I need check/run this code every time

Sag47 hit it right on the head...if you don't have a second means of connecting to the Internet, the only way you'll ever get the alert email, is when your Internet connection is WORKING. If you *DO* have a second means of Internet connectivity...your script will STILL fail, since if one connection goes down, your ping command will still work, thereby causing your 'Internet-is-down' check to fail. Unless you have an alternate means of Internet communications, or your script has more than those two lines and is doing alot more processing, what you're trying to do seems like it would never work.
Quote:

Originally Posted by karim.ouda
if you want it should run in regular interval,use crontab and define your time interval so it will run according to that.

Right...sag47 already gave that advice about cron.

karim.ouda 06-14-2013 01:12 PM

Quote:

Originally Posted by TB0ne (Post 4971870)

Right...sag47 already gave that advice about cron.

actually there is only 4 min difference between my post and his post and when i was typing on my device he had posted already that's why i couldnt see his reply.

selvavinayak88 06-14-2013 01:50 PM

Thanks sam,karim.ouda,TB0ne for suggesting crontab... I am looking on that friends...

after ur's suggestion I created like, for every 5 min email will send to my mail "internet is up". if, i am not getting this mail i take steps to checks internet connection (Alert signal) for every 5Min . is this way is correct ? i am waiting for reply....


Thanks,
Selvavinayakam.na

sag47 06-14-2013 01:51 PM

Quote:

Originally Posted by TB0ne (Post 4971870)
If you *DO* have a second means of Internet connectivity...your script will STILL fail, since if one connection goes down, your ping command will still work, thereby causing your 'Internet-is-down' check to fail.

I forgot to mention that. Good point. You can add hops to your ping command and force it through a specific gateway if you have an alternate internet connection.

http://superuser.com/questions/31184...ernate-gateway

**EDIT**

Quote:

Originally Posted by selvavinayak88 (Post 4971944)
after ur's suggestion I created like, for every 5 min email will send to my mail "internet is up". if, i am not getting this mail i take steps to checks internet connection (Alert signal) for every 5Min . is this way is correct ? i am waiting for reply....

That sounds like a really painful heartbeat mechanism. You'll be getting almost 300 emails a day.

If I were you I would have a second server off site (hosting maybe?) which gives an outside perspective of your internet. That outside server can then email you or text message you.

One example would be connecting via public key auth over SSH to a remote server on hosting and outputting a timestamp to a file.

Then you can read that file and if the timestamp is older than 310 seconds then it means the server hasn't checked in in longer than 5 minutes. It would certainly provide a lot less chatter in your email and you would only be notified if it is important.

SAM

selvavinayak88 06-14-2013 01:56 PM

Thanks Sam, I got Right way to explore new things....


Selvavinayakam.na

TB0ne 06-14-2013 02:10 PM

Quote:

Originally Posted by sag47 (Post 4971945)
I forgot to mention that. Good point. You can add hops to your ping command and force it through a specific gateway if you have an alternate internet connection.

http://superuser.com/questions/31184...ernate-gateway
**EDIT**
That sounds like a really painful heartbeat mechanism. You'll be getting almost 300 emails a day.

If I were you I would have a second server off site (hosting maybe?) which gives an outside perspective of your internet. That outside server can then email you or text message you.

One example would be connecting via public key auth over SSH to a remote server on hosting and outputting a timestamp to a file.

Then you can read that file and if the timestamp is older than 310 seconds then it means the server hasn't checked in in longer than 5 minutes. It would certainly provide a lot less chatter in your email and you would only be notified if it is important.

+1 for that solution...hadn't even considered that, good call sag47.

selvavinayak88 06-14-2013 02:30 PM

oK, I accept your point of view...


Selvavinayakam.na


Quote:

Originally Posted by sag47 (Post 4971945)
I forgot to mention that. Good point. You can add hops to your ping command and force it through a specific gateway if you have an alternate internet connection.

http://superuser.com/questions/31184...ernate-gateway

**EDIT**



That sounds like a really painful heartbeat mechanism. You'll be getting almost 300 emails a day.

If I were you I would have a second server off site (hosting maybe?) which gives an outside perspective of your internet. That outside server can then email you or text message you.

One example would be connecting via public key auth over SSH to a remote server on hosting and outputting a timestamp to a file.

Then you can read that file and if the timestamp is older than 310 seconds then it means the server hasn't checked in in longer than 5 minutes. It would certainly provide a lot less chatter in your email and you would only be notified if it is important.

SAM



All times are GMT -5. The time now is 06:50 PM.