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


Groups > comp.lang.python > #35031

Re: os.system and subprocess odd behavior

References <bb08fc10-85f0-4003-a1fb-cdc449299d19@googlegroups.com> <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>
Date 2012-12-18 00:01 +0000
Subject Re: os.system and subprocess odd behavior
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1004.1355790699.29569.python-list@python.org> (permalink)

Show all headers | View raw


On 17 December 2012 20:56, py_genetic <conor.robinson@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
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