2016-09-25 2 views
0

私のスクリプトでos.system()を使ってシェルコマンドを呼び出しています。 os.system()がコマンドをどのように実行するかを理解するためのサンプルスクリプトを作成しました。Python:linuxとfreeBSDのos.systemの動作を理解する

import os 
os.system("sleep 20") 

Iは、上記の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 

シェルは両方のマシンでbashです。

os.system(cmd)はサブシェルでcmdを実行するので、sh -c sleep 20プロセスもfreeBSD上で実行しないでください。誰かがこの行動を説明できますか?

+1

'os.system()'ので、あなたの答えがあるでしょう、基本的にシステム(3) ''のためのパススルーです。 –

答えて

1

"sh"はLinuxとFreeBSDでは同じではありません。 Linuxの/ bin/shはfork()コールを行いますが、FreeBSDの/ bin/shはcommandを実行するために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. 
関連する問題