LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-12-2014, 10:38 AM   #1
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Rep: Reputation: 47
How to cron something to run with a delay and only if a ping fails execute command


Hi everyone. I want to cron something @reboot on a RHEL box, but what I want it to do is have it wait a minute, then if pings to a certain IP fail, or it to restart networking on the machine (/etc/init.d/network restart).

Only problem is I don't know how to automate the wait 1 minute then if ping fails to 1.2.3.4 then /etc/init.d/network restart.
 
Old 08-12-2014, 01:35 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,681
Blog Entries: 14

Rep: Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022
Create a script that contains the following (you could name it something like pingtest.sh):

Code:
#!/bin/bash
sleep 60
if /bin/ping -c 3 <ip>
then /sbin/service network restart
fi
Be sure to put your actual IP in the place of <ip> above.

Make the script executable and put it somewhere secure such as /root/scripts.

Modify root's cron to call the script only at reboot:

Code:
@reboot /root/script/pingtest.sh
Of course you could add more to the script such as logging or an "else" for what you'd like it to do if the ping fails. You can also add logging to the cron entry.
 
1 members found this post helpful.
Old 08-12-2014, 01:39 PM   #3
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
Thanks!

I only want it to do the restart if the pings fail though, so how would I make that if part work like that?
 
Old 08-12-2014, 02:42 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,681
Blog Entries: 14

Rep: Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022
Code:
if ! /bin/ping -c 3 <ip>
The exclamation point means "not".
 
1 members found this post helpful.
Old 08-12-2014, 03:00 PM   #5
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
cool thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
working script fails when run by cron adayforgotten Linux - Server 8 04-03-2013 01:50 PM
[SOLVED] ffmpeg fails to run in cron but ok from command line Liquid_Squelch Linux - Software 7 02-11-2013 05:29 PM
[SOLVED] Cron run fails at random points, throws: Attempting to re-run cron while.... Nighteyes Linux - Newbie 2 06-22-2012 06:08 AM
cron backup job fails to run bluethundr Linux - Newbie 5 01-06-2011 06:50 AM
cron file 000-delay.cron (it is on my FC5) - what is the purpose? jtmoon Linux - Server 1 03-08-2007 12:15 PM


All times are GMT -5. The time now is 02:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration