popen

subprocess.popen detached from master (Linux)

萝らか妹 提交于 2020-01-02 11:07:34
问题 I am trying to open a subprocess but have it be detached from the parent script that called it. Right now if I call subprocess.popen and the parent script crashes the subprocess dies as well. I know there are a couple of options for windows but I have not found anything for *nix. I also don't need to call this using subprocess. All I need is to be able to cal another process detached and get the pid. 回答1: With linux, it's no issue at all. Just Popen() . For example, here is a little dying

Python subprocess.popen() without waiting

◇◆丶佛笑我妖孽 提交于 2020-01-02 08:22:30
问题 I'm using Python 3.4.2 on Windows. In script1.py I'm doing this: myProc = subprocess.Popen([sys.executable, "script2.py", "argument"]) myProc.communicate() it works and call script2.py . The problem is that in script2.py there is a infinite loop (there must be) and the script1.py is waiting for script2.py to finish. How can I tell to script1.py to just call script2.py and don't wait for the process to finish? 回答1: Just don't call myProc.communicate() if you don't want to wait. subprocess

unsuccessful use of popen() in C?

坚强是说给别人听的谎言 提交于 2020-01-02 06:42:14
问题 I can run the following command xwd -root | xwdtopnm | pnmtojpeg > screen.jpg in a terminal under linux and it will produce a screenshot of my current screen. I try to do the following with the code: #include <stdio.h> #include <stdlib.h> int main() { FILE *fpipe; char *command="xwd -root | xwdtopnm | pnmtojpeg"; char line[256]; if ( !(fpipe = (FILE*)popen(command,"r")) ) { // If fpipe is NULL perror("Problems with pipe"); exit(1); } while ( fgets( line, sizeof line, fpipe)) { //printf("%s",

Real time output of subprocess.popen() and not line by line

一笑奈何 提交于 2020-01-02 05:09:49
问题 I'm currently rewriting a little wrapper program in python that I once wrote in C++. It extracts files from a file and boxes them in another format. In C++ the output from the system commands I need to run was "real time" i.e the status bar and the percentage indicator of some commands where shown in real time. With python I get each 'percent' dumped on the screen individually (because I read it line by line). Here's an example: Thats how a status bar looks in the python version (this goes on

Creating fstream object from a FILE* pointer

南笙酒味 提交于 2020-01-01 23:54:13
问题 The well known way of creating an fstream object is: ifstream fobj("myfile.txt"); ie. using a filename. But I want to create an ifstream object using a file descriptor. Reason: I want to execute a command using _popen() . _popen() returns the output as a FILE* . So there is a FILE* pointer involved but no filename. 回答1: You cannot do that just in standard C++, since iostreams and C I/O are entirely separate and unrelated. You could however write your own iostream that's backed by a C FILE

Creating fstream object from a FILE* pointer

回眸只為那壹抹淺笑 提交于 2020-01-01 23:53:38
问题 The well known way of creating an fstream object is: ifstream fobj("myfile.txt"); ie. using a filename. But I want to create an ifstream object using a file descriptor. Reason: I want to execute a command using _popen() . _popen() returns the output as a FILE* . So there is a FILE* pointer involved but no filename. 回答1: You cannot do that just in standard C++, since iostreams and C I/O are entirely separate and unrelated. You could however write your own iostream that's backed by a C FILE

How to filter a lot of data with IPC::Open2?

浪尽此生 提交于 2020-01-01 19:12:30
问题 My task is to filter some data from perl script with external utility (the addr2line). The data size is quite large. I need to print a lot of data to stdin of program and read a lot of data back (from stdout of program into my script). Now I do this with IPC::Open2 , but I don't mix reading and writing. Is this legal? Will Open2 buffer any size of data in pipe? My code: my $cmd="addr2line -e $prog_name "; use IPC::Open2; local (*Reader, *Writer); my $pid = open2(\*Reader, \*Writer, $cmd); for

Reading stdout process in real time

久未见 提交于 2019-12-31 12:31:10
问题 Let's consider this snippet: from subprocess import Popen, PIPE, CalledProcessError def execute(cmd): with Popen(cmd, shell=True, stdout=PIPE, bufsize=1, universal_newlines=True) as p: for line in p.stdout: print(line, end='') if p.returncode != 0: raise CalledProcessError(p.returncode, p.args) base_cmd = [ "cmd", "/c", "d:\\virtual_envs\\py362_32\\Scripts\\activate", "&&" ] cmd1 = " ".join(base_cmd + ['python -c "import sys; print(sys.version)"']) cmd2 = " ".join(base_cmd + ["python -m http

python check_output fails with exit status 1 but Popen works for same command

会有一股神秘感。 提交于 2019-12-30 03:34:54
问题 Command framed to identify if Xcode is running on Mac: cmd = "ps -ax | grep -v grep | grep Xcode" If Xcode is not running, then above command works well with Popen method of subprocess module, but raises a CalledProcessError with check_output method. I tried to inspect the stderr through the following code, but failed to get appropriate information to understand the reason. from subprocess import check_output, STDOUT, CalledProcessError psCmd = "ps -ax | grep -v grep | grep Xcode" o = None

常见Python中调用Linux命令

隐身守侯 提交于 2019-12-29 07:26:43
有时候我们需要从Python中执行Linux命令,下面就介绍几种方法? 1. os 模块:   1.1 os.system 函数:     system方法会创建子进程运行外部程序,方法只返回外部程序的运行结果。这个方法比较适用于外部程序 没有输出结果的情况 。 >>> os.system('echo \ "Hello World\"') # 直接使用os.system调用一个echo命令 Hello World 0 >>> os.system("cat /proc/meminfo") MemTotal: 486640 kB MemFree: 301184 kB Buffers: 10304 kB Cached: 58316 kB SwapCached: 0 kB Active: 43748 kB Inactive: 45336 kB Active(anon): 20476 kB Inactive(anon): 520 kB Active(file): 23272 kB Inactive(file): 44816 kB     注意:上面说了,此方法只会外部程序的结果,也就是os.system的结果,所以如果你想接收命令的返回值,接着向下看   1.2 os模块的popen方法     当需要得到外部程序的输出结果时,本方法非常有用, 返回一个类文件对象 ,调用该对象的 r ead