Hello,
I’ve got Ruby scripts that have been working fine for years now. But,
every now and then, one of my scripts errors out with a “permission
denied” error. This is very frustrating. Usually, it involves moving a
file to either a sister directory or a subdirectory. And, like I said,
99% of the time it works fine. Has anyone else experienced this? It kind
of scares me because it’s only been happening for me with Ruby. It
didn’t use to happen with Perl, and, believe, I don’t want to go back to
Perl.
I’ve got Ruby scripts that have been working fine for years now. But,
every now and then, one of my scripts errors out with a “permission
denied” error. This is very frustrating. Usually, it involves moving a
file to either a sister directory or a subdirectory. And, like I said,
99% of the time it works fine. Has anyone else experienced this? It kind
of scares me because it’s only been happening for me with Ruby. It
didn’t use to happen with Perl, and, believe, I don’t want to go back to
Perl.
Can you post your code? Not sure, but trying to move ‘.’ or ‘…’ on a
Unix system might cause this problem. Also, did you verify that you
have right permissions on the files, and directories?
I’ve got Ruby scripts that have been working fine for years now. But,
every now and then, one of my scripts errors out with a “permission
denied” error. This is very frustrating. Usually, it involves moving a
file to either a sister directory or a subdirectory. And, like I said,
99% of the time it works fine. Has anyone else experienced this? It kind
of scares me because it’s only been happening for me with Ruby. It
didn’t use to happen with Perl, and, believe, I don’t want to go back to
Perl.
Can you post your code? Not sure, but trying to move ‘.’ or ‘…’ on a
Unix system might cause this problem. Also, did you verify that you
have right permissions on the files, and directories?
Yes, I know I have the rights. It’s on a Windows server. And, I’m doing
a “FileUtils.mv” command. And, like I said, it works 99% of the time.
Cheers.
You can also try to strace your script. In the logs you’ll find the
system
call causing the Permission denied…
strace? Hmmm. Never heard of that. I’ll have to look into it.
Given that you’re on Windows, you don’t have strace; however, Process
Monitor from SysInternals
(Process Monitor - Sysinternals | Microsoft Learn) should be
able to do much the same operation.
The likely problem though is that some other process, or even that
running your script, still has one of the files or directories you’re
trying to move open and in use. Common culprits are virus scanners, so
if you have one of those, you might try temporarily disabling it and see
if that eliminates your problem. In rare cases, it is necessary to
remove such offending software entirely.
-Jeremy
I don’t have the right to disable the virus scanner on this server.
That’s controlled by IT guys. I’ll look into the SysInternals thing.
I’ve used their stuff before. Thanks.
You can also try to strace your script. In the logs you’ll find the
system
call causing the Permission denied…
strace? Hmmm. Never heard of that. I’ll have to look into it.
Given that you’re on Windows, you don’t have strace; however, Process
Monitor from SysInternals
(Process Monitor - Sysinternals | Microsoft Learn) should be
able to do much the same operation.
The likely problem though is that some other process, or even that
running your script, still has one of the files or directories you’re
trying to move open and in use. Common culprits are virus scanners, so
if you have one of those, you might try temporarily disabling it and see
if that eliminates your problem. In rare cases, it is necessary to
remove such offending software entirely.
I don’t have the right to disable the virus scanner on this server.
That’s controlled by IT guys.
Ask them if they have recently updated or reconfigured the virus scanner
or any similar file scanning service on the server. It’s possible that
they may make further changes to work around the issue if you can
convince them that their most recent changes broke something important.
The nutshell here is that something – possibly the virus scanner or
possibly some other software – is touching the file when your script
runs, altering the permissions such that your script can’t do what
it’s trying to do.
Just to be clear. It’s not that something is changing the ACLs on the
file or anything. Rather, a file or directory that is in use by another
program cannot be removed or relocated on Windows without some ugly
trickery. This is a limitation of Windows itself.
I know that Cygwin implements some of the hacks to make it at least
appear possible to move and delete in-use files. You could try running
your scripts with Cygwin’s Ruby package to see if that helps things, but
be aware that the performance as a whole will likely suffer a bit.
That’s controlled by IT guys. I’ll look into the SysInternals thing.
I’ve used their stuff before. Thanks.
The nutshell here is that something – possibly the virus scanner or
possibly some other software – is touching the file when your script
runs, altering the permissions such that your script can’t do what
it’s trying to do. The problem isn’t something unique to or endemic to
Ruby – it’s just a fundamental permissions issue. The solution is
simple. You just need to handle the exception that is generated.
begin
Your code here.
FileUtils.mv(“a”,“b”)
rescue Errno::EACCES
Well, dang. No permissions. So, do something to deal with it.
You could sleep and ‘retry’, though you probably want to implement a
counter if you do that so that you aren’t stuck in a loop forever.
You could just write the offending file to a log somewhere.
I don’t have the right to disable the virus scanner on this server.
That’s controlled by IT guys.
Ask them if they have recently updated or reconfigured the virus scanner
or any similar file scanning service on the server. It’s possible that
they may make further changes to work around the issue if you can
convince them that their most recent changes broke something important.
Right. I was just being intentionally general in my phrasing, since
the advice is applicable to anyone writing code to move files around,
regardless of OS.
Yeah, I figured as much. I just wanted to make sure that others reading
this in the forum later would be sure to understand this issue in
particular. Given the description of the problem, you probably wouldn’t
ever see this kind of error on a Unix system unless some program
actually did change the permissions on the file or directory itself
rather than just have it open as is likely the case here.
The nutshell here is that something – possibly the virus scanner or
possibly some other software – is touching the file when your script
runs, altering the permissions such that your script can’t do what
it’s trying to do.
Just to be clear. It’s not that something is changing the ACLs on the
file or anything. Rather, a file or directory that is in use by another
program cannot be removed or relocated on Windows without some ugly
trickery. This is a limitation of Windows itself.
Right. I was just being intentionally general in my phrasing, since
the advice is applicable to anyone writing code to move files around,
regardless of OS.
Kirk H.
Unhandled exceptions are ugly. Make the world more beautiful, and
handle yours today.
I’m a little
concerned about his maybe just being a Windows problem.
What is the concern exactly? People have had to deal with the inability
to remove or relocate in-use files and directories on Windows for quite
a long time, so there is usually a way to avoid the cause of the issue
once identified. For instance, if the AV software is actually the
problem, the fix may be as simple as configuring the AV software to
avoid scanning in the directory structure in which your script operates.
Right. I was just being intentionally general in my phrasing, since
the advice is applicable to anyone writing code to move files around,
regardless of OS.
Yeah, I figured as much. I just wanted to make sure that others reading
this in the forum later would be sure to understand this issue in
particular. Given the description of the problem, you probably wouldn’t
ever see this kind of error on a Unix system unless some program
actually did change the permissions on the file or directory itself
rather than just have it open as is likely the case here.
-Jeremy
I’ll just put in some exception handling, as suggested. And, I’ll ask my
IT guys if the virus scanner might be a culprit here. I’m a little
concerned about his maybe just being a Windows problem.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.