• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

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.
  • 3
  • 2
1 Solution
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.


[ -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

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.

Change %Y to %X, ie:

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

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

thanks a lot.
if you have lsof installed on your system, you'll get more accurate results.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now