简体   繁体   English

为什么在 lsyncd.conf 中 getenv ("HOME") 等于 nil?

[英]Why is getenv ("HOME") equal to nil in lsyncd.conf?

I am trying to configure lsyncd to synchronize a folder on the home page of the logged user but when I try to capture the value of his $HOME with os.getenv("HOME") the result is always我正在尝试配置 lsyncd 以同步登录用户主页上的文件夹,但是当我尝试使用os.getenv("HOME")捕获他的$HOME的值时,结果总是

Error prepare /etc/lsyncd/lsyncd.conf.lua: /etc/lsyncd/lsyncd.conf.lua: 14: attempt to concatenate a nil value错误准备 /etc/lsyncd/lsyncd.conf.lua: /etc/lsyncd/lsyncd.conf.lua: 14: 尝试连接 nil 值

I tried os.getenv("PWD") and it runs without displaying errors but it doesn't work and in the /var/log/lsyncd/lsyncd.status file I see that it tries to use the /.mozilla/firefox address as if PWD were empty.我试过os.getenv("PWD")并且它运行时不显示错误,但它不起作用,在/var/log/lsyncd/lsyncd.status文件中我看到它试图使用/.mozilla/firefox地址好像 PWD 是空的。

I tried to keep the environment variables by running the command sudo -E service lsyncd restart and modifying sudoers with Defaults env_keep +="HOME" , all in vain.我试图通过运行命令sudo -E service lsyncd restart并使用Defaults env_keep +="HOME"修改 sudoers 来保留环境变量,但都是徒劳的。
Any ideas?有任何想法吗?

I enclose my code:我附上我的代码:

settings{
    logfile = "/var/log/lsyncd/lsyncd.log",
    statusFile = "/var/log/lsyncd/lsyncd.status",
}

sync{
        default.rsync,
        source =  os.getenv("HOME").."/.mozilla/firefox/",
        target =  "tmp/.mozilla/firefox/",
        delay  = 1,
}

Linux does not have a concept of “the logged-in user”. Linux 没有“登录用户”的概念。 It has a concept of a logged-in user , but there can be many of them at the same time.具有的概念已登录的用户,但可以在同一时间有很多人。

You're running a system service, through systemd .您正在通过systemd运行系统服务。 Telling sudo to preserve the HOME environment variable means that when you run sudo -E service lsyncd restart , the service command runs with HOME set to the home directory of the user who called sudo .告诉sudo保留HOME环境变量意味着当您运行sudo -E service lsyncd restartservice命令运行时将HOME设置为调用sudo的用户的主目录。 But that has nothing to do with the value of HOME for the service.但这与HOME对服务的价值无关。 Systemd does not set the environment of a service based on the environment of the administrative command to start it, they're based on the service configuration. Systemd 不会根据启动它的管理命令的环境来设置服务的环境,而是基于服务配置。

If you want to synchronize a specific user's files, then hard-code the path to that user's home directory, or use getpwuid or getpwnam to look up the home directory of a user.如果要同步特定用户的文件,请对该用户主目录的路径进行硬编码,或使用getpwuidgetpwnam查找用户的主目录。

If you want to synchronize the files of whichever user is logged in, then don't use a system service.如果要同步任何登录用户的文件,请不要使用系统服务。 Instead, run lsyncd as part of that user's login session.相反,将 lsyncd 作为该用户登录会话的一部分运行。

Thank you Gilles for you answer.谢谢吉尔斯的回答。 Unfortunately getpwuid and getpwnam returned "nil"values, equivalent to the null de lua.不幸的是 getpwuid 和 getpwnam 返回“nil”值,相当于 null de lua。 The solution I have developed allows me to run lsyncd in a multi-user and multi-device environment with synchronization with active directory, so that I can save the firefox markers, google chrome, svn settings, filezilla, history... in the active directory and recover them on any computer for any user.我开发的解决方案允许我在与活动目录同步的多用户和多设备环境中运行 lsyncd,这样我就可以在活动中保存 firefox 标记、谷歌浏览器、svn 设置、filezilla、历史...目录并为任何用户在任何计算机上恢复它们​​。 This is complicated by the problem of compatibility with symbolic links between the samba dialect, cifs, which uses active directory and linux.与使用活动目录和 linux 的 samba 方言 cifs 之间的符号链接的兼容性问题使这变得复杂。 My solution is as follows:(just for Firefox by saving space but can be extended to any software or folder. config.)我的解决方案如下:(仅适用于Firefox通过节省空间但可以扩展到任何软件或文件夹。配置。)

1- I install lsyncd and add that scrip in /etc/lsyncd/lsyncd.conf.lua (The script looks for the home of the user who has just logged on to the /tmp/home file and uses it to create the url with the source and target to synchronize.) 1-我安装 lsyncd 并在 /etc/lsyncd/lsyncd.conf.lua 中添加该脚本(该脚本查找刚刚登录到 /tmp/home 文件的用户的家,并使用它来创建带有要同步的源和目标。)

function file_exists(file)
  local f = io.open(file, "rb")
  if f then f:close() end
  return f ~= nil
end

-- get all lines from a file, returns an empty 
-- list/table if the file does not exist
function lines_from(file)
  if not file_exists(file) then return {} end
  lines = {}
  for line in io.lines(file) do
    lines[#lines + 1] = line
  end
  return lines
end

-- tests the functions above
local file = '/tmp/lua'
local lines = lines_from(file)

-- print all line numbers and their contents
for k,v in pairs(lines) do
  print('line[' .. k .. ']', v)

end
--save line 1 in home
home = lines[1]


settings{

    logfile = "/var/log/lsyncd/lsyncd.log",
    statusFile = "/var/log/lsyncd/lsyncd.status",
}

sync{

        default.rsync,
-- Add home to url of the source
        source =  home.."/.mozilla/firefox/",
--Add home to url of the target
        target =  home.."/box/.mozilla/firefox/",
        delay  = 1,
        rsync={
                perms = true,
                owner = true,


        },
}

2- With visudo I allow the users I want, in my case all of them, to run lsyncd without password with sudo command: ALL ALL=NOPASSWD:SETENV: /usr/sbin/service lsyncd * 2- 使用 visudo,我允许我想要的用户(在我的情况下是所有用户)使用 sudo 命令在没有密码的情况下运行 lsyncd:ALL ALL=NOPASSWD:SETENV: /usr/sbin/service lsyncd *

3- I add the following lines in /etc/profile file 3- 我在 /etc/profile 文件中添加以下几行

//Delete .mozilla in user's local home in start
rm -Rf $HOME/.mozilla
#mkdir -p $HOME/box/.mozilla/Firefox
//Box is the folder shared with the server
mkdir -p $HOME/box/.config
rm -rf $HOME/box/.config/caja
//2> /dev/null prevents an error due to any other cause from being shown to the user and prevents the user from starting up.
cp -rf $HOME/box/.mozilla  $HOME/  2> /dev/null
cp -rf $HOME/box/.config $HOME/ 2> /dev/null
cp -rf $HOME/box/.subversion $HOME/ 2> /dev/null
echo $HOME >> /tmp/lua;
sudo service lsyncd restart; rm -r /tmp/lua

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

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