简体   繁体   English

拒绝具有Rake权限的移动文件

[英]Move file with Rake permission denied

I have a Rake at the end of which the server moves the processed file from a folder to another one, using FileUtils.mv , like this: 我有一个Rake,在结尾处,服务器使用FileUtils.mv将已处理的文件从一个文件夹移动到另一个文件夹,如下所示:

FileUtils.mv('/path-to-upload-folder/'+filename, '/path-to-imported-folder/'+filename) if File.exist?('/path-to-upload-folder/'+filename)

If i run this command from within the rails server (I have an action that is a copy of the rake task, just for simplicity of debugging inside a controller), everything goes fine (probably because I run the server with root privileges with rvmsudo). 如果我从Rails服务器中运行此命令(我的操作是rake任务的副本,只是为了简化控制器内部的调试),一切都会正常(可能是因为我以rvmsudo的root特权运行服务器) 。 When running from the Rake task, I get a permission denied error, like this 从Rake任务运行时,出现权限被拒绝的错误,像这样

Errno::EACCES: Permission denied @ sys_fail2 - 

The source folder is called uploads, and the destination folder is the imported folder. 源文件夹称为上载,目标文件夹称为导入的文件夹。 Following the permissions and the user and groups of the folder 遵循权限以及文件夹的用户和组

drwxr-xr-x  2 malatini malatini   4096 lug 14 14:26 imported/
drwxr-xr-x  2 www-data www-data 135168 lug 14 14:26 uploads/

where malatini is my current user. malatini是我当前的用户。 I know that for running a raw mv from the two folders I need to be a sudo user, but why can I run the same command from within the Rails servers without any problem? 我知道要从两个文件夹运行原始mv,我需要成为sudo用户,但是为什么我可以在Rails服务器中运行相同的命令而没有任何问题呢?

I also tried to change permissions and owners/groups of the destination folder, but with no luck. 我也尝试更改权限和目标文件夹的所有者/组,但是没有运气。

Reading here I suppose the problem is the user that is running the rake task. 在这里阅读我想问题是运行rake任务的用户。 The same problem happens either if I manually run the rake task, either if it is run as a cron job. 如果我手动运行rake任务,或者作为cron作业运行,都会发生相同的问题。

I am running under 我在下奔跑

Debian 3.16.7-ckt11-1 (2015-05-24) x86_64 GNU/Linux

Any suggestion? 有什么建议吗?

EDIT: 编辑:

As @Nic Nilov suggested I tried to change owner and group of source and destination foleders, and actually I managed to perform the mv, and hence rake task, changing owner and group of both folders to malatini 正如@Nic Nilov所建议的那样,我试图更改所有者和源文件夹和目标文件夹的组,实际上我设法执行了mv并因此执行了rake任务,将两个文件夹的所有者和组都更改为malatini

drwxr-xr-x  2 malatini malatini   4096 lug 14 14:26 imported/
drwxr-xr-x  2 malatini malatini 135168 lug 14 14:26 uploads/

but in this way, apache (which is in charge of moving files to uploads folder) is not able to write to uploads folder. 但是以这种方式,apache(负责将文件移动到上载文件夹)无法写入上载文件夹。 No other configuration is working (not just the group of uploads folder, nor the 777 to imported folder). 没有其他配置有效(不仅是上载文件夹组,也不是777到导入的文件夹)。

You basically answered your own question. 您基本上回答了自己的问题。 This is a permission issue, related to the OS user under which the move operation is attempted. 这是一个权限问题,与尝试进行移动操作的OS用户有关。 It works from Rails since, as you say, you run it with rvmsudo . 它可以在Rails中运行,因为正如您所说,您是使用rvmsudo运行它的。

Two ways around that would be to either run your rake task from a privileged user or set ownership on both folders such that the current user you run rake under is allowed to perform mv . 解决该问题的两种方法是从特权用户运行rake任务,或者在两个文件夹上设置所有权,以便允许在其下运行rake的当前用户执行mv

On your folders both user and group set to be the same. 在文件夹上,用户和组都设置为相同。 You could set their group to the group of the user running rake , eg: 您可以将他们的组设置为运行rake的用户的组,例如:

chgrp malatini ./uploads

This would make malatini group the owner of both folders: 这将使malatini组成为两个文件夹的所有者:

drwxr-xr-x  2 malatini malatini   4096 lug 14 14:26 imported/
drwxr-xr-x  2 www-data malatini 135168 lug 14 14:26 uploads/

Which should allow the mv operation. 应该允许mv操作。

UPDATE 更新

When running rake under a privileged user and doing that from cron and to avoid keeping the password stored anywhere you can use NOPASSWD directive. 在特权用户下运行rake并从cron执行此操作时,为了避免将密码存储在任何地方,可以使用NOPASSWD指令。

See for more details this askubuntu answer . 有关更多详细信息,请参见askubuntu的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM