Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'output': 0.04; 'root': 0.04; 'sysadmin': 0.05; 'bash': 0.07; 'correct.': 0.07; 'permissions': 0.07; 'python': 0.09; 'cc:addr:googlegroups.com': 0.09; 'clean.': 0.09; 'cmd': 0.09; 'mem': 0.09; 'permissions.': 0.09; 'cc:addr:python-list': 0.10; '(other': 0.16; 'cc:name:python list': 0.16; 'help?': 0.16; 'no)': 0.16; 'subprocess': 0.16; 'sudo': 0.16; 'wrote:': 0.17; 'byte': 0.17; 'tests': 0.18; 'permission': 0.20; 'file.': 0.20; 'written': 0.20; 'skip:- 40': 0.21; 'sort': 0.21; 'denied': 0.22; 'runs': 0.22; "i'd": 0.22; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'cc:2**1': 0.24; 'command': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'possibly': 0.27; 'errors.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'fine': 0.28; 'run': 0.28; 'cat': 0.29; 'src': 0.29; "i'm": 0.29; 'waste': 0.30; 'december': 0.32; 'file': 0.32; 'zero': 0.33; 'problem': 0.33; 'received:google.com': 0.34; 'doing': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'created': 0.36; 'except': 0.36; 'but': 0.36; 'correctly': 0.37; 'does': 0.37; 'level': 0.37; 'rather': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'fact': 0.38; 'some': 0.38; 'nothing': 0.38; 'called': 0.39; 'header:Received:5': 0.40; 'share': 0.61; 'more': 0.63; 'great': 0.64; 'touch': 0.69; 'oscar': 0.84; 'way...': 0.84; 'piping': 0.91; 'skip:/ 30': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=X02WS30af6zo1CiDf5L4CZmqzmJMq2Ud61WHHd4Vnyc=; b=wCIXGOOf1n4d46pvMpYyuHQhi95gpLOii0cvSqLKSHFSoA1xkeuBvo+0TsCX9ZQLkX vEM6paE2vc5lXm9wFd18omTRRG16C6nOeAtMlUWPG7yeC/a3QR0b7JvaZ3hC8A5/fUYr J9BpTHh8tQRvze/Q2eKYCjDd5u/jycTPVbIkcK3IfDKgjgDBNhv/9KYEJmPIAa5hfpGo uokN+KfLhT9lcA3YgQw0b8/4mem1TxFfyOAtW2vnJI15H+C1i1lcn4W/BBUJYFvAr90+ Nd8+e26a3Mbp1/Mot68aOITFy7ksJf/4sAOgsAXAam8S4tKpx21ir2EX+IfnZeJ0ZPOd 1DRw== MIME-Version: 1.0 In-Reply-To: <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> References: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> Date: Tue, 18 Dec 2012 00:01:07 +0000 Subject: Re: os.system and subprocess odd behavior From: Oscar Benjamin To: py_genetic Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Python List , comp.lang.python@googlegroups.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355790699 news.xs4all.nl 6982 [2001:888:2000:d::a6]:55767 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35031 On 17 December 2012 20:56, py_genetic wrote: > Oscar, seems you may be correct. I need to run this program as a superus= er. However, after some more tests with simple commands... I seem to be w= orking correctly from any permission level in python.... Except for the out= put 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. Howeve= r, 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=3D/usr/local/Calpont/m= ysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL > /home/my= usr/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 fil= e 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 fo= r 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