# "permission denied" happening too often

**URL:** <https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888>\
**Category:** Ruby\
**Created:** [February 8, 2011, 2:06pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888 "2011-02-08T14:06:39Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 8, 2011, 2:06pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/1 "2011-02-08T14:06:39Z")

</div>

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.

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2011, 2:32pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/2 "2011-02-08T14:32:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2011, 2:36pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/3 "2011-02-08T14:36:43Z")

</div>

You can also try to strace your script. In the logs you’ll find the  
system  
call causing the Permission denied…

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2011, 2:40pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/4 "2011-02-08T14:40:24Z")

</div>

Markus S. wrote in post #980289:

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

Thanks.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2011, 2:39pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/5 "2011-02-08T14:39:23Z")

</div>

Anurag P. wrote in post #980286:

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2011, 2:48pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/6 "2011-02-08T14:48:46Z")

</div>

Jeremy B. wrote in post #980293:

> On 02/08/2011 07:40 AM, Peter B. wrote:
> 
> > Markus S. wrote in post #980289:
> > 
> > > 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](http://technet.microsoft.com/en-us/sysinternals/bb896645)) 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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2011, 2:44pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/7 "2011-02-08T14:44:53Z")

</div>

On 02/08/2011 07:40 AM, Peter B. wrote:

> Markus S. wrote in post #980289:
> 
> > 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](http://technet.microsoft.com/en-us/sysinternals/bb896645)) 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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2011, 4:25pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/8 "2011-02-08T16:25:27Z")

</div>

On 2/8/2011 7:48 AM, Peter B. wrote:

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

-Jeremy

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2011, 4:29pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/9 "2011-02-08T16:29:13Z")

</div>

On 2/8/2011 8:45 AM, Kirk H. wrote:

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

-Jeremy

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2011, 3:49pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/10 "2011-02-08T15:49:01Z")

</div>

On Tue, Feb 8, 2011 at 6:48 AM, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

# 

end

Kirk H.  
Engine Y.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2011, 4:59pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/11 "2011-02-08T16:59:50Z")

</div>

Jeremy B. wrote in post #980325:

> On 2/8/2011 7:48 AM, Peter B. wrote:
> 
> > 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.
> 
> -Jeremy

I’ll do that. Thanks.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 9, 2011, 2:39am UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/12 "2011-02-09T02:39:58Z")

</div>

On 02/08/2011 09:36 AM, Kirk H. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 8, 2011, 4:37pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/13 "2011-02-08T16:37:24Z")

</div>

On Tue, Feb 8, 2011 at 8:28 AM, Jeremy B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 2/8/2011 8:45 AM, Kirk H. wrote:
> 
> > 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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 9, 2011, 5:10pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/14 "2011-02-09T17:10:36Z")

</div>

On 02/09/2011 08:31 AM, Peter B. wrote:

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

-Jeremy

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 9, 2011, 3:31pm UTC](https://www.ruby-forum.com/t/permission-denied-happening-too-often/202888/15 "2011-02-09T15:31:46Z")

</div>

Jeremy B. wrote in post #980452:

> On 02/08/2011 09:36 AM, Kirk H. wrote:
> 
> > 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.
