I have a bash script that uses ping testing for internet access, that has a cron job initiating the script with a 2>&1 output of any issues.
However, I need those issues, to write a specific line in the log file that I’m using. No route to host and cannot resolve host do get captured, but that would indicate an issue getting to internet services and as such, should be logged in my log file.
I am really struggling to find a solution.
*/1 * * * * /onlineCHECK/onlineCHECK.sh >> /onlineCHECK/logs/onlineCHECK_issues--Crontab.log 2>&1
What I would like is it to write the following to my logfile:
(date "+%m/%d/%Y %I:%M:%S %p")"|0|0|0|0|"(date "+%m/%d/%Y %I:%M:%S %p")
5
Pipe your command to a loop that prefixes each line with the date. Create another script date_prefix.sh
:
#!/bin/bash
while read -r line
do
echo "$(date "+%m/%d/%Y %I:%M:%S %p")|$line"
done
In crontab:
*/1 * * * * /onlineCHECK/onlineCHECK.sh 2>&1 | date_prefix >> /onlineCHECK/logs/onlineCHECK_issues--Crontab.log 2>&1
1