English 中文(简体)
页: 1
原标题:Python os.execve()
  • 时间:2017-10-03 15:25:36
  •  标签:
  • python-2.7

I m 尝试采用execve()的新工艺,从os模块中选取。 我需要启动新的进程,在另一个司中做一些 st子,但我还是取得了一些变化。

code:

import os

os.execve( /bin/ls , [ /bin/ls ], { PATH :  /tmp })

i 操作该代码时,即从<代码>/tmp的目录上看一米。 什么是错的? 我不能使用<代码>os.chdir()或改变指挥方式(如使用模块subprocess)

问题回答

Change your current working directory before calling execve(). The CWD will be inherited by the new process.

import os
os.chdir( /tmp )
os.execve( /bin/ls , [ arg1 ,  arg2 ], { PATH :  is_inherited_anyway })

如果有<代码>/bin/sh,可:

import os
target_dir =  /tmp 
your_target_command_with_args =  ls  # Just put the arguments for the command space-separated in this string, as if this string were your system s default shell.
os.execve( /bin/sh , [ /bin/sh ,  -c , f cd "{target_dir}"; {your_target_command_with_args} ], {})

如果具体涉及<代码>/bin/ls,你实际上可以提出这一论点(其他一些方案也支持作为论据的道路),例如:

import os
target_dir =  /tmp 
os.execve( /bin/ls , [ /bin/ls , target_dir], {})

I think your approach didn t work because ls just doesn t read the PATH envvar to determine what directory to list, which you set in the third argument (when I omitted that, os.execve complained about the argument env (position 3) missing).

Also, the PATH envvar normally contains a colon-separated list of directories in which a command is searched for, like /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin. If you enter the command ls in any shell it searches in each of these directories for a file called ls and the first one it finds is then run.





相关问题
Curve fitting with lmfit for Mossbauer spectroscopy

Im pretty new to lmfit and I keep running into this error. My dataset is pretty large ~27000 points of data from I gather: my error msg import numpy as np import matplotlib.pyplot as plt from lmfit ...

Python SocketServer: sending to multiple clients?

Well, I m trying to build a small python prgram with a SocketServer that is supposed to send messages it receives to all connected clients. I m stuck, I don t know how to store clients on the ...

Is there no mysql connector for python 2.7 on windows

While I see a bunch of links/binaries for mysql connector for python 2.6, I don t see one for 2.7 To use django, should I just revert to 2.6 or is there a way out ? I m using windows 7 64bit django ...

How to update the image of a Tkinter Label widget?

I would like to be able to swap out an image on a Tkinter label, but I m not sure how to do it, except for replacing the widget itself. Currently, I can display an image like so: import Tkinter as ...

PyScripter for Python 2.7

How to run PyScripter if you have Python 2.7 installed? There is a command line parameter for Pyscipter to tell it which pythonXX.dll to use, but I can t get this working.

How to set time limit on raw_input

in python, is there a way to, while waiting for a user input, count time so that after, say 30 seconds, the raw_input() function is automatically skipped?

热门标签