[Tutor] os.system() problem

Eric Brunson brunson at brunson.com
Tue Feb 5 17:11:38 CET 2008


Aditya Lal wrote:
> On 04/02/08 10:42 PM, "Eric Brunson" <brunson at brunson.com> wrote:
>
>   
>> dave selby wrote:
>>     
>>> Hi all,
>>>
>>> I am not sure if this is a Python or bash issue :).
>>>
>>> In bash if I execute 'motion' with the following ...
>>>
>>> dave at dev-machine:~/.kde/share/apps/kmotion$ motion &> /dev/null &
>>> [1] 10734
>>> dave at dev-machine:~/.kde/share/apps/kmotion$
>>>
>>> I get what I expect, a background job, however if I execute it from
>>> Python with an os.system ...
>>>
>>> os.system('motion &> /dev/null &')
>>>   
>>>       
>> This happens because &> and & are shell constructs, they are bash
>> specific shell syntax for "redirect stderr and stdout" and "put this job
>> in the background".  But os.system simply calls the OS's "system(3)"
>> call, which under linux calls "/bin/sh".  If you read the docs for bash,
>> calling it as "sh" results in POSIX compliance mode and falls back to
>> Bourne shell's less rich syntax, so it doesn't understand the "&>"
>> construct.  If I had to guess at the parsing, I imagine it runs the
>> "motion &" as one process in the background, then "> /dev/null &" as a
>> second.
>>
>> Long story short, look at this page:
>> http://docs.python.org/lib/node537.html
>>
>>     
>>> I get tons of output to the BASH shell ...
>>>
>>> [0] Processing thread 0 - config file /etc/motion/motion.conf
>>> [0] Processing config file /etc/motion/motion.1.conf
>>> [0] Processing config file /etc/motion/motion.2.conf
>>> [1] Thread is from /etc/motion/motion.1.conf
>>> [2] Thread is from /etc/motion/motion.2.conf
>>> [1] Thread started
>>> [2] Thread started
>>> [1] File of type 2 saved to: /var/lib/motion/20080203/01/tmp/175253.jpg
>>> ...etc ...
>>>
>>> I just can't work out why this is happening & how to stop it ?. Any ideas ?
>>>
>>> Cheers
>>>
>>> Dave
>>>
>>>
>>>       
>
> Try os.system('bash motion &> /dev/null &')
>   


I doubt that would work.  The command line would still be parsed by 
/bin/sh, so it would execute "bash motion &", then "> /dev/null &".  
Maybe if you did something like:

os.system( 'bash -c "motion &> /dev/null &" ' )

Or you could just use Popen, which is the more appropriate solution.

>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   



More information about the Tutor mailing list