在/ etc / network / interfaces中为pre-down命令设置PYTHONPATH的位置?

时间:2014-07-04 11:19:38

标签: python bash environment-variables

如何让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),以及在import语句之前操作sys.path。我要求学习:)

1 个答案:

答案 0 :(得分:0)

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

的环境