Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35067

Re: os.system and subprocess odd behavior

Newsgroups comp.lang.python
Date 2012-12-18 10:26 -0800
References (1 earlier) <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> <mailman.970.1355764606.29569.python-list@python.org> <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> <mailman.1004.1355790699.29569.python-list@python.org>
Subject Re: os.system and subprocess odd behavior
From py_genetic <conor.robinson@gmail.com>
Message-ID <mailman.1026.1355861274.29569.python-list@python.org> (permalink)

Show all headers | View raw


Oscar I can confirm this behavior from terminal. 

AND this works as well, simulating exactly what I'm doing permissions wise, and calling sudo python test.py  below

f1 = open('TESTDIR/file1.txt', 'w')
f1.write('some test here\n')
f1.close()

cmd1 = 'cat < TESTDIR/file1.txt > TESTDIR/file2.txt'
P = Popen(cmd1, shell=True)
P.wait()

cmd2 = 'cat < TESTDIR/file1.txt | sudo tee TESTDIR/file3.txt'
P = Popen(cmd2, shell=True)
P.wait()

-rw-r--r-- 1 root root       15 Dec 18 12:57 file1.txt
-rw-r--r-- 1 root root       15 Dec 18 12:57 file2.txt
-rw-r--r-- 1 root root       15 Dec 18 12:57 file3.txt

HOWEVER... 

when using this command from before.... no dice

/usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL > /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT

OR

/usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL | sudo tee /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT

So it's basically as if python gets a response instantly (perhaps from the query) and closes the process, since we've verified its not permissions related.

Perhaps someone can try a mysql cmd line such as above within python?  And see if you can verify this behavior.  I believe the query returning with no errors is shutting the sub shell/process?

I've tried this with all options p.wait() ect as well as parsing the command and running shell false.

Again the exact command run perfect when pasted and run from the shell.  I'll try running it a few other ways with some diff db options.


> Follow through the bash session below
> 
> 
> 
> $ cd /usr
> 
> $ ls
> 
> bin  games  include  lib  local  sbin  share  src
> 
> $ touch file
> 
> touch: cannot touch `file': Permission denied
> 
> $ sudo touch file
> 
> [sudo] password for oscar:
> 
> $ ls
> 
> bin  file  games  include  lib  local  sbin  share  src
> 
> $ cat < file > file2
> 
> bash: file2: Permission denied
> 
> $ sudo cat < file > file2
> 
> bash: file2: Permission denied
> 
> $ sudo cat < file > file2
> 
> bash: file2: Permission denied
> 
> $ sudo cat < file | tee file2
> 
> tee: file2: Permission denied
> 
> $ sudo cat < file | sudo tee file2
> 
> $ ls
> 
> bin  file  file2  games  include  lib  local  sbin  share  src
> 
> 
> 
> The problem is that when you do
> 
> 
> 
>   $ sudo cmd > file2
> 
> 
> 
> it is sort of like doing
> 
> 
> 
>   $ sudo cmd | this_bash_session > file2
> 
> 
> 
> so the permissions used to write to file2 are the same as the bash
> 
> session rather than the command cmd which has root permissions. By
> 
> piping my output into "sudo tee file2" I can get file2 to be written
> 
> by a process that has root permissions.
> 
> 
> 
> I suspect you have the same problem although it all complicated by the
> 
> fact that everything is a subprocess of Python. Is it possibly the
> 
> case that the main Python process does not have root permissions but
> 
> you are using it to run a command with sudo that then does have root
> 
> permissions?
> 
> 
> 
> Does piping through something like "sudo tee" help?
> 
> 
> 
> 
> 
> Oscar

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

os.system and subprocess odd behavior py_genetic <conor.robinson@gmail.com> - 2012-12-14 10:13 -0800
  Re: os.system and subprocess odd behavior Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-14 22:47 +0000
    Re: os.system and subprocess odd behavior py_genetic <conor.robinson@gmail.com> - 2012-12-17 08:39 -0800
      Re: os.system and subprocess odd behavior Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-12-17 17:16 +0000
        Re: os.system and subprocess odd behavior py_genetic <conor.robinson@gmail.com> - 2012-12-17 12:56 -0800
          Re: os.system and subprocess odd behavior Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-12-18 00:01 +0000
            Re: os.system and subprocess odd behavior py_genetic <conor.robinson@gmail.com> - 2012-12-18 10:26 -0800
              Re: os.system and subprocess odd behavior Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-12-18 18:52 +0000
                Re: os.system and subprocess odd behavior py_genetic <conor.robinson@gmail.com> - 2012-12-18 12:52 -0800
                Re: os.system and subprocess odd behavior py_genetic <conor.robinson@gmail.com> - 2012-12-18 12:52 -0800
            Re: os.system and subprocess odd behavior py_genetic <conor.robinson@gmail.com> - 2012-12-18 10:26 -0800
          Re: os.system and subprocess odd behavior Hans Mulder <hansmu@xs4all.nl> - 2012-12-18 23:46 +0100
        Re: os.system and subprocess odd behavior py_genetic <conor.robinson@gmail.com> - 2012-12-17 12:56 -0800
  Re: os.system and subprocess odd behavior Dieter Maurer <dieter@handshake.de> - 2012-12-15 08:12 +0100
    Re: os.system and subprocess odd behavior py_genetic <conor.robinson@gmail.com> - 2012-12-17 08:30 -0800
    Re: os.system and subprocess odd behavior py_genetic <conor.robinson@gmail.com> - 2012-12-17 08:30 -0800
  Re: os.system and subprocess odd behavior photonymous@gmail.com - 2012-12-17 21:10 -0800
    Re: os.system and subprocess odd behavior Hans Mulder <hansmu@xs4all.nl> - 2012-12-18 11:27 +0100
      Re: os.system and subprocess odd behavior Dave Angel <d@davea.name> - 2012-12-18 05:39 -0500
        Re: os.system and subprocess odd behavior Hans Mulder <hansmu@xs4all.nl> - 2012-12-18 12:40 +0100

csiph-web