2016-09-25 64 views
0

我在脚本中使用os.system()来调用shell命令。我写了一个示例脚本来了解os.system()如何执行命令。Python:了解os.system在linux和freeBSD上的行为

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的) :

root  32726 0.0 0.0 4440 648 pts/2 S+ 01:01 0:00 sh -c sleep 20 
root  32727 0.0 0.0 7192 612 pts/2 S+ 01:01 0:00 sleep 20 

壳牌在这两台机器中都遭受打击。

由于os.system(cmd)在子shell中执行cmd,不应该有一个在freeBSD上运行的sh -c sleep 20进程吗?有人可以解释这种行为吗?

+1

'使用os.system()'本质上是'系统(3)'直通,所以你的答案将在那里。 –

回答

1

“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. 

FreeBSD的:

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.