I have a directory with crash logs, and I'd like to use a conditional statement in a bash script based on a find command.
The log files are stored in this format:
/var/log/crashes/app-2012-08-28.log
/var/log/crashes/otherapp-2012-08-28.log
I want the if statement to only return true if there is a crash log for a specific app which has been modified in the last 5 minutes. The find command that I would use is:
find /var/log/crashes -name app-\*\.log -mmin -5
I'm not sure how to incorporate that into an if statement properly. I think this might work:
if [ test `find /var/log/crashes -name app-\*\.log -mmin -5` ] then
service myapp restart
fi
There are a few areas where I'm unclear:
- I've looked at the if flags but I'm not sure which one, if any, that I should use.
- Do I need the
testdirective or should I just process against the results of the find command directly, or maybe usefind... | wc -lto get a line count instead? - Not 100% necessary to answer this question, but
testis for testing against return codes that commands return? And they are sort of invisible - outside ofstdout/stderr? I read themanpage but I'm still pretty unclear about when to usetestand how to debug it.
find ... -exec. Also see the example commands under Why is looping over find's output bad practice?... -exec command ';' -quit, but I don't believe there is any solution for the latter other than parsing the result. Also, in either case, the primary problem with parsing the result offind(i.e. inability to distinguish delimiters from characters in filenames) doesn't apply, as you don't need to find delimiters in these cases.if find ... | grep .is better: unix.stackexchange.com/a/684153/43233