在哪里为/etc/network/interfaces中的predown命令设置PYTHONPATH?

2024-03-28 17:53:56 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

在my/etc/network/interfaces with/usr/local/myscripts/pre中_向下.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。我问是为了学习:)


Tags: pyimporthomemyusrlocalscriptssudo
1条回答
网友
1楼 · 发布于 2024-03-28 17:53:56

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的环境

相关问题 更多 >