Help with Shell Scripting

I am trying to write a shell script that looks for  a file in  a specified directory which is not being used. It is  a log file  which gets written sometimes.  So I need an o/p something like this

return code 1 - If the file is found and not used

return code 2- If the file is not found

return code 3 - if the file is found but is being written.

Can someone please help me on this.
bumblebee500Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:
The following code assumes that if the file hasn't been written to after 60 seconds, then it isn't being used.  Adjust the time to whatever suits your environment.

#!/bin/bash
LOG=/path/to/your/file.log

[ -f $LOG ] || exit 2

now=$(date +%s)
mod=$(stat -c %Y $LOG)
let age="now - mod"

[ $age -gt 60 ] && exit 1

exit 3

Open in new window

0
bumblebee500Author Commented:
thanks Tintin

Can you please suggest me some modifications, i understand that this script is checking for the timestamp, suppose lets say if this file is copied by other user at the same time, can I get the status then.

So in short I also want to know if this file is accessed by other user. Sorry for not being clear the first time.


Best
0
TintinCommented:
Change %Y to %X, ie:

mod=$(stat -c %X $LOG)

this will give the time of last access rather than time of last modification.
0
bumblebee500Author Commented:
Ok I will try this and will let you know.

thanks a lot.
0
TintinCommented:
if you have lsof installed on your system, you'll get more accurate results.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.