【问题标题】:Where to set PYTHONPATH for pre-down command in /etc/network/interfaces?在 /etc/network/interfaces 中为 pre-down 命令设置 PYTHONPATH 的位置?
【发布时间】:2014-07-04 11:19:37
【问题描述】:

我如何让sudo -E 真正保护它的环境。假设我有一个

pre-down python /usr/local/myscripts/pre_down.py

在我的/etc/network/interfaces with/usr/local/myscripts/pre_down.py`中

import a

/home/user/scripts/a.py

print 'a'

/home/user/scripts/__init__.py 存在,为什么sudo PYTHONPATH=/home/user/scripts ifdown eth1 会导致

Traceback (most recent call last):
  File "/usr/local/myscripts/pre_down.py", line 1, in <module>
    import a
ImportError: No module named a

?我知道编写一个 sh 脚本作为包装器可以解决问题(尝试并成功使用内容PYTHONPATH=/home/user/scripts /usr/local/myscripts/pre_down.py),以及在导入语句之前操作sys.path。我问是为了学习:)

【问题讨论】:

    标签: python bash environment-variables


    【解决方案1】:

    interfacesman-page 声明:

       All of these commands have access to the following environment variables.
    
       IFACE  physical name of the interface being processed
    
       LOGICAL
              logical name of the interface being processed
    
       ADDRFAM
              address family of the interface
    
       METHOD method of the interface (e.g., static)
    
       MODE   start if run from ifup, stop if run from ifdown
    
       PHASE  as per MODE, but with finer granularity, distinguishing the pre-up, post-up, pre-down and post-down phases.
    
       VERBOSITY
              indicates whether --verbose was used; set to 1 if so, 0 if not.
    
       PATH   the command search path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    
       Additionally, all options given in an interface definition stanza are exported 
       to the  environment  in  upper  case  with "IF_" prepended and with hyphens
       converted to underscores and non-alphanumeric characters discarded.
    

    所以这些脚本似乎没有继承命令ifupifdown的环境

    【讨论】:

      猜你喜欢
      • 2013-07-06
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 2015-05-01
      相关资源
      最近更新 更多