LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ping script for internet connection (https://www.linuxquestions.org/questions/linux-newbie-8/ping-script-for-internet-connection-4175696253/)

ytd 06-10-2021 05:47 AM

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.

shruggy 06-10-2021 05:53 AM

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.

ytd 06-10-2021 07:00 AM

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!

ondoho 06-11-2021 02:17 AM

Quote:

Originally Posted by ytd (Post 6257897)
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 (Post 6257915)
The thread can now be closed!

You can do it yourself. See my signature, marking a thread SOLVED.


All times are GMT -5. The time now is 01:26 PM.