BashScripts.org
http://bashscripts.org/forum/

Script - ping finally answers and cp
http://bashscripts.org/forum/viewtopic.php?f=15&t=860
Page 1 of 1

Author:  djmicz [ Wed Aug 05, 2009 1:56 pm ]
Post subject:  Script - ping finally answers and cp

Hi and welcome (it's my first post),

I have a simple script:

#!/bin/bash
host="192.168.0.1"
ping -c 2 $host 1>/dev/null
num=$?
if [ $num -eq 0 ]; then
exit
else
cp /home/user/backup/test.odt /home/user/Desktop/
DISPLAY=:0.0 /usr/bin/xmessage "Please restart"
fi

Condition is: If ping returns no error exit, if there is error cp test.odt
Script above works.

If the situation will that ping will be with errors (else condition)
i would like to add another condition: if ping finally will be without error cp sth.txt.

Any help?

Regards,

Janusz

Author:  thobbs [ Thu Aug 06, 2009 1:36 am ]
Post subject:  Re: Script - ping finally answers and cp

Hi Janusz, welcome to the forum!

It is difficult to understand what you are asking. Would you please explain "if ping finally will be without error"? Do you want to wait for the computer to be restarted and then try to ping again?

Author:  djmicz [ Thu Aug 06, 2009 2:25 am ]
Post subject:  Re: Script - ping finally answers and cp

Whata i need sctipt to do is:

In cron script will run every 2 minutes:

1) Check ip if it answers
2) If yes exit
3)if not:

- copy file1.txt
- xmessage notification

If ip finally answers copy file2.txt

Regards,

Janusz

Author:  thobbs [ Thu Aug 13, 2009 11:17 pm ]
Post subject:  Re: Script - ping finally answers and cp

Why don't you add this to your else:
Code:
else
  cp /home/user/backup/test.odt /home/user/Desktop/
  DISPLAY=:0.0 /usr/bin/xmessage "Please restart"

  sleep 90 s   # Sleep one and a half minutes

  ping -c 2 $host 1>/dev/null   # Try to ping again
  num=$?
  if [ $num -eq 0 ]
    cp /home/.../file2 /home/.../Destination  # Fill in with correct paths
  fi
fi

Since cron will run every two minutes, it doesn't make sense to wait any longer than 90 seconds or so, but any time shorter than that would work.

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com