Python:理解linux和freeBSD上os.system的行为
我在脚本中使用os.system()调用shell命令。我编写了一个示例脚本来了解os.system()如何执行该命令Python:理解linux和freeBSD上os.system的行为,python,linux,python-2.7,freebsd,os.system,Python,Linux,Python 2.7,Freebsd,Os.system,我在脚本中使用os.system()调用shell命令。我编写了一个示例脚本来了解os.system()如何执行该命令 import os os.system("sleep 20") 我在freeBSD和linux机器上运行了上述代码,然后执行了ps aux | grep sleep,结果如下: freeBSD: :~]# ps aux | grep sleep root 94832 0.0 0.0 2768 984 0 S+ 5:31AM 0:00.00 slee
import os
os.system("sleep 20")
我在freeBSD和linux机器上运行了上述代码,然后执行了ps aux | grep sleep,结果如下:
freeBSD:
:~]# ps aux | grep sleep
root 94832 0.0 0.0 2768 984 0 S+ 5:31AM 0:00.00 sleep 20
linux(ubuntu):
在这两台机器中,Shell都是bash
既然os.system(cmd)
在子shell中执行cmd
,那么在freeBSD上不也应该有sh-c sleep 20
进程运行吗?有人能解释一下这种行为吗?“sh”在Linux和FreeBSD上是不一样的。Linux的/bin/sh进行fork()调用,但FreeBSD的/bin/sh进行execve()调用以执行命令,因此它不会生成Linux中显示的新进程
Linux:
sh-4.1$ sh --version
sh --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
os.system()
本质上是system(3)
的传递,因此您的答案将在那里。
sh-4.1$ sh --version
sh --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
man sh
DESCRIPTION
The sh utility is the standard command interpreter for the system. The
current version of sh is close to the IEEE Std 1003.1 (“POSIX.1”)
specification for the shell. It only supports features designated by
POSIX, plus a few Berkeley extensions. This man page is not intended to
be a tutorial nor a complete specification of the shell
HISTORY
A sh command, the Thompson shell, appeared in Version 1 AT&T UNIX. It was
superseded in Version 7 AT&T UNIX by the Bourne shell, which inherited the
name sh.
This version of sh was rewritten in 1989 under the BSD license after the
Bourne shell from AT&T System V Release 4 UNIX.
AUTHORS
This version of sh was originally written by Kenneth Almquist.