[Tutor] os.system() problem
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 &
>>>  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
>> Long story short, look at this page:
>>> I get tons of output to the BASH shell ...
>>>  Processing thread 0 - config file /etc/motion/motion.conf
>>>  Processing config file /etc/motion/motion.1.conf
>>>  Processing config file /etc/motion/motion.2.conf
>>>  Thread is from /etc/motion/motion.1.conf
>>>  Thread is from /etc/motion/motion.2.conf
>>>  Thread started
>>>  Thread started
>>>  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 ?
> 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
More information about the Tutor