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 06-10-2021, 05:47 AM   #1
ytd
Member
 
Registered: Jan 2009
Posts: 205

Rep: Reputation: 31
ping script for internet connection


Hi,

I have this script:


#!/bin/bash

host=google.com

if [ -z $host ]; then
echo "Usage: $(basename "$0") [HOST]"
exit 1
fi

START=$(date +%s)
while [ $(( $(date +%s) - 360 )) -lt "$START" ]; do #<-- this pings the google.com for 6 min.


result=$(ping -W 1 -c 1 $host | grep 'bytes from ')
if [ $? -gt 0 ]; then
echo -e "$(date +'%Y/%m/%d %H:%M:%S') - host $host is \033[0;31mdown\033[0m"
else
echo -e "$(date +'%Y/%m/%d %H:%M:%S') - host $host is \033[0;32mok\033[0m -$(echo "$result" | cut -d ':' -f 2)"
sleep 1 # avoid ping rain

fi


done



put it in crontab because I wanned it to start at a specific time - 22.50 because at that time something is happening and my internet goes down and I want to check this automatically with a script.

50 22 * * * root /home/myuser/ping6.sh >> /home/myuser/monitorizare/google.com.log$(date +%H:%M_%d-%m-%Y)


If I run this script manually:

/home/myuser/ping6.sh >> /home/myuser/monitorizare/google.com.log$(date +%H:%M_%d-%m-%Y)


it creates in the logfile:

021/06/10 13:33:26 - host google.com is ^[[0;32mok^[[0m - icmp_seq=1 ttl=110 time=33.2 ms
2021/06/10 13:33:27 - host google.com is ^[[0;32mok^[[0m - icmp_seq=1 ttl=110 time=33.8 ms
2021/06/10 13:33:28 - host google.com is ^[[0;32mok^[[0m - icmp_seq=1 ttl=110 time=40.0 ms
2021/06/10 13:33:29 - host google.com is ^[[0;32mok^[[0m - icmp_seq=1 ttl=110 time=33.2 ms
2021/06/10 13:33:30 - host google.com is ^[[0;32mok^[[0m - icmp_seq=1 ttl=110 time=34.1 ms

but while running via crontab, I receive the following error message:

vi /var/spool/mail/root:

Date: Thu, 10 Jun 2021 12:45:01 +0300 (EEST)

/bin/bash: -c: line 0: unexpected EOF while looking for matching `)'
/bin/bash: -c: line 1: syntax error: unexpected end of file

And it doesn't create my logfile.

Why? Can someone help me, please? It's wierd because when I ran it manually it's all good.


I also checked with: https://www.shellcheck.net/ and this script have no errors (except at line 15)
but in cron log it says unexpected EOF while looking for matching `)'

Help, please.
 
Old 06-10-2021, 05:53 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 2,319

Rep: Reputation: Disabled
The percent sign is special in a crontab, escape it with a backslash:
Code:
50 22 * * * root /home/myuser/ping6.sh >> /home/myuser/monitorizare/google.com.log$(date +\%H:\%M_\%d-\%m-\%Y)
From the crontab(5) man page:
Quote:
The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or a "%" character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. A "%" character in the command, unless escaped with a backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

Last edited by shruggy; 06-10-2021 at 05:58 AM.
 
1 members found this post helpful.
Old 06-10-2021, 07:00 AM   #3
ytd
Member
 
Registered: Jan 2009
Posts: 205

Original Poster
Rep: Reputation: 31
Thank you very much!

I have modified like this and it's working!


50 22 * * * root /home/myuser/ping8.sh >> /home/myuser/monitorizare/`date +\%Y-\%m-\%d_\%H:\%M:\%S`-google.com.log

The thread can now be closed!
 
Old 06-11-2021, 02:17 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,237
Blog Entries: 10

Rep: Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160
Quote:
Originally Posted by ytd View Post
result=$(ping -W 1 -c 1 $host | grep 'bytes from ')
if [ $? -gt 0 ]; then
...
If your only wish is to check if there is an internet connection (to a particular host), I would rather do it like this:

Code:
if ping -W 1 -c 1 "$host"; then
...
Simpler?

You can even discard ping's output:
Code:
if ping -W 1 -c 1 "$host" >/dev/null 2>&1; then
...
Quote:
Originally Posted by ytd View Post
The thread can now be closed!
You can do it yourself. See my signature, marking a thread SOLVED.
 
  


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
Strange Ping Issue - Can't ping localhost but can ping others on LAN code_slinger Linux - Networking 15 03-30-2015 02:39 PM
I cannot ping with command 'ping IP' address but can ping with 'ping IP -I eth0' sanketmlad Linux - Networking 2 07-15-2011 05:32 AM
ping connection to router OK, but no ping between boxes in LAN mac1234mac Linux - Networking 2 05-26-2010 11:29 AM
Ping PING Ping uid0sd Linux - Networking 1 02-13-2007 06:50 PM
Can ping network but can't ping any internet ip when I hard set the ip Streme Linux - Wireless Networking 4 04-13-2004 09:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:34 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
Open Source Consulting | Domain Registration