ping script for internet connection
by ytd from LinuxQuestions.org on (#5JWT3)
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.
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.