gaganso
gaganso

Reputation: 3011

Python: Understanding the behaviour of os.system on linux and freeBSD

I am using os.system() in my script to call a shell command. I wrote a sample script to understand how os.system() executes the command.

import os
os.system("sleep 20")

I ran the above code on a freeBSD and linux machine and then did ps aux | grep sleep, the results were as below:

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

Shell is bash in both the machines.

Since os.system(cmd) executes cmd in a subshell, shouldn't there be an sh -c sleep 20 process running on freeBSD too? Can someone please explain this behaviour?

Upvotes: 1

Views: 301

Answers (1)

Michael Zhilin
Michael Zhilin

Reputation: 563

"sh" is not same on Linux and FreeBSD. Linux's /bin/sh makes fork() call, but FreeBSD's /bin/sh makes execve() call to execute command, so it doesn't produce new process shown in 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.

Upvotes: 2

Related Questions