os.system and subprocess odd behavior

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Dec 18 01:01:07 CET 2012

On 17 December 2012 20:56, py_genetic <conor.robinson at gmail.com> wrote:
> Oscar, seems you may be correct.  I need to run this program as a superuser.  However, after some more tests with simple commands...  I seem to be working correctly from any permission level in python.... Except for the output write command from the database to a file.  Which runs fine if I paste it into the cmd line.  Also, subprocess.call_check() returns clean.  However, nothing is written to the output file when called from python.
> so this cmd runs great from the cmd line (sudo or no) however the output file in this case is owned by the sysadmin either way... not root?
> /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
> When run from sudo python (other files are also created and owned by root correctly)  however no output is written from the db command zero byte file only (owned by root)... returns to python with no errors.
> I'm sorta at a loss.  I'd rather still avoid having python connect to the db directly or reading the data from stdout, is a waste or mem and time for what I need.

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

Does piping through something like "sudo tee" help?


More information about the Python-list mailing list