I have a script using the following command to remove a folder:

ssh -q root@hostname sh -c 'rm -rf /opt/jetty/'

Most of the times it works fine, but occasionally I get this message:

rm: cannot remove `/opt/jetty': Directory not empty

The only suspicious point is the folder to delete contains a lock file which is being used by shell command flock to acquire file lock. But I tried to remove the folder manually when the file was even being locked by flock, it get succeeded.


The environment is Linux

# uname -a
Linux XXX 2.6.32-504.8.1.el6.x86_64 #1 SMP Fri Dec 19 12:09:25 EST 2014 x86_64 x86_64 x86_64 GNU/Linux

rm didn't alias to anything, the out put of rm:

# rm
rm: missing operand
Try `rm --help' for more information.

I have a service script to start Jetty which creates a file lock inside the folder. Some snippet as shown below:


    #open file descriptor and acquire lock
    exec 200>$JETTY_LOCK
    flock -x -n 200 || { exec 200>&-;echo 'jetty is being started by another process.'; sleep 5; exit 0; }
    cd /opt/jetty/
    echo $!>&200
    flock -u 200  #release lock
    exec 200>&-   #close file descriptor


After the error reproduced, I found only jetty_lock file was still remaining int /opt/jetty, does that mean flock stops file lock from deleting? I cannot provide more details because I don't know when will it happen the next time...

  • Are new files commonly created in that directory? – Michael Homer May 5 '15 at 9:18
  • hi @MichaelHomer, no new files except the lock file is created in that directory. – 夜神ライト May 5 '15 at 9:22
  • Is this on Linux? Which one? Is rm aliased to anything? What is the output of type rm? Please edit your question and add this information. – terdon May 5 '15 at 10:05
  • 1
    The other thing that you can tell people is whether you can reproduce this when your jetty service is not running. After all, jetty has a whole temporary files directory in /opt/jetty/temp, for starters. – JdeBP May 5 '15 at 11:24
  • If you change the command to ssh -q root@hostname sh -c 'rm -rf /opt/jetty/ || ls -la /opt/jetty/', does it output anything? – Mark Plotnick May 5 '15 at 13:46

To debug ssh session use "-v" option instead of "-q" and analize the output.

Try to use absolute paths in remote command. So this looks like:

# ssh -v root@hostname /bin/sh -c '/bin/rm -rf /opt/jetty/'
| improve this answer | |
  • or use ssh -vvv for lots of detail – Skaperen May 5 '15 at 10:42
  • 4
    This question is a race between an rm command and a running jetty service that is using/creating files and directories in the directory being removed. It's not really addressed by looking at the SSH protocol or giving a different name for the shell. Both of those are obviously working. – JdeBP May 5 '15 at 11:10
  • hi @JdeBP , do you mean the file lock cannot be removed by 'rm -rf'? – 夜神ライト May 6 '15 at 1:45
  • When you use sh instead of bash you have different alias. So you have to use absolute path. I don't know your environment, but /bin/rm -fr have to function. Are you sure remote filesystem is not in read-only mode? – alex87alex May 7 '15 at 8:12
  • hi @alex87alex I'm sure remote filesystem is not in read-only mode because all the files in the folder have been deleted except the one serves as file lock. – 夜神ライト May 8 '15 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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