os.system and subprocess odd behavior

py_genetic conor.robinson at gmail.com
Tue Dec 18 13:26:25 EST 2012


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




More information about the Python-list mailing list