check date_filename.txt exist

53 Views Asked by At

I've created a set of linux command that do set of ping and keep the log file into 2 file, which is date-time_successping and date-time_failping. I want to do an action where when date-time_failping exist, the linux system will send an email to the PIC. but the filename is too random since i've a date infront of it. E.g. 20170911-160455_failping. I tried using like -c/-f, but they cannot search if no specific name. there will be multiples of file with variant of dates created. So I need some advice on this. hope anyone could help.

thanks

P/S: I'm so sorry if the information that given is not enough, please reply if needed more info so that i could try to provide and help me solve this issue.

Regards

2

There are 2 best solutions below

0
On

You can do it something like this optionally,

This command is to get yesterday date:

VAR1=`date +%Y%m%d -d "yesterday"`

Or you want to work with today's date

VAR1=`date +%Y%m%d `
$file = VAR1`__failping`
if [ -f "$file" ]
then
    echo "My message" | mail -s subject [email protected]
else
    //Do something else
end
0
On

You can use inotify to monitor your log file creation event, then check if the file pattern match xxxxx_failping. Assume your log files are put in ping_logs, you can use the following script to monitor

$ inotifywait -rme create ping_logs/ |awk '{if($NF ~ /[0-9]+-[0-9]+_failping/) print $NF}'

Here just print the file name, you can change to your mailing action.