Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is related to an effort to shutdown a seldom used server if there are no SSH connections or open files.

How can I determine in a shell script (cron job on a Linux box) if any of the current samba shares have any open files?

share|improve this question

If your samba server is configured to use file locking which is probably is, I suppose you could use 'smbstatus -L' to list the locks and then grep the output for the type of lock you're looking for (EXCLUSIVE, BATCH) if you knew you only cared about certain kinds of locks.

Something like:

smbstatus -L | grep EXCLUSIVE

and then test against the return value ($?) in the cron job script.

Unfortunately, I don't know enough about the types of locks to know if this is a suitable catch-all method or not.

share|improve this answer

I would probably use the lsof command to determine what files are in use on the share.

lsof | grep /path/to/share should show files in use.

share|improve this answer

cense's answer is good, and his thing about the locks applies to this answer. Samba also allows read only status (I think it shows up as RD_ONLY), and also displays entries for directories that are open in Windows Explorer, so you have to decide what kind of locks you want to ignore/focus on.

I believe the output of smbstatus is No machines connected., if, well, no machines are connected, so you could possibly use something like smbstatus|grep No\ machines\ connected && shutdown to shutdown when nothing's connected.

To the best of my knowledge (No access to a Samba server right now), if you have a Samba share mounted in Windows, smbstatus will show that the Windows machine has connected, even if the user isn't actively using the share, so you'll probably need to decide what to do based on that case too.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.