44

I'm running this:

os.system("/etc/init.d/apache2 restart")

It restarts the webserver, as it should. However, it also outputs a message like so (like it would if I had run the command directly from the terminal)

* Restarting web server apache2                                              ...
waiting                                                             [ OK ]

How can I disable this output?

6
  • 5
    os.system("/etc/init.d/apache2 restart >/dev/null") will discard that output. As Noufal has said, subprocess is preferred. If you're wanting to make a quick adjustment to pre-existing code, though, redirecting to /dev/null might be an attractive option. Commented Apr 8, 2011 at 15:09
  • 1
    @kirk: why a comment rather than a answer ? Commented Apr 8, 2011 at 15:32
  • 1
    @mb14: I didn't think it was as "correct" as the recommendations to use subprocess. I thought of it as more of a side note, like "while I'm not exactly suggesting you do this, here's another idea." Commented Apr 8, 2011 at 15:50
  • related: How to hide output of subprocess in Python 2.7 Commented Aug 12, 2015 at 0:56
  • this worked for me because i was looking for something which works in python 2 and 3. Commented Aug 11, 2016 at 13:37

6 Answers 6

69

Avoid os.system() by all means, and use subprocess instead:

with open(os.devnull, 'wb') as devnull:
    subprocess.check_call(['/etc/init.d/apache2', 'restart'], stdout=devnull, stderr=subprocess.STDOUT)

This is the subprocess equivalent of the /etc/init.d/apache2 restart &> /dev/null.

There is subprocess.DEVNULL on Python 3.3+:

#!/usr/bin/env python3
from subprocess import DEVNULL, STDOUT, check_call

check_call(['/etc/init.d/apache2', 'restart'], stdout=DEVNULL, stderr=STDOUT)
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for an actual example that shows how to solve the OPs problem using subprocess. :)
43

Depending on your OS (and that's why as Noufal said, you should use subprocess instead) you can try something like

 os.system("/etc/init.d/apache restart > /dev/null")

or (to mute also the error)

os.system("/etc/init.d/apache restart > /dev/null 2>&1")

Comments

26

You should use the subprocess module using which you can control the stdout and stderr in a flexible fashion. os.system is deprecated.

The subprocess module allows you to create an object which represents a running external process. You can read it from it's stdout/stderr, write to it's stdin, send it signals, terminate it etc. The main object in the module is Popen. There are a bunch of other convenience methods like call etc. The docs are very comprehensive and include a section on replacing the older functions (including os.system).

7 Comments

Awesome. Thanks for answering. Which function would it be though? The docs confuse me. call? popen?
+10 for an incomplete answer! This shows that voting and quality don't go hand in hand :-) . Noufal, you could mention the differences.
popen but you should take some time and read through the docs. I imagine you've only skimmed through them. They're quite clear really.
Do you have a source explicitly marking it as deprecated, as opposed to "not preferred"? Guido is on record as being opposed to its removal. I'm not disagreeing with your answer - subprocess is so much nicer! - just clarifying a point.
Guido is generally against changing the standard library. The docs for os.system indicate that it's "preferable" to use subprocess.
|
2

Here is a system call function I pieced together several years ago and have used in various projects. If you don't want any output from the command at all you can just say out = syscmd(command) and then do nothing with out.

Tested and works in Python 2.7.12 and 3.5.2.

def syscmd(cmd, encoding=''):
    """
    Runs a command on the system, waits for the command to finish, and then
    returns the text output of the command. If the command produces no text
    output, the command's return code will be returned instead.
    """
    p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
        close_fds=True)
    p.wait()
    output = p.stdout.read()
    if len(output) > 1:
        if encoding: return output.decode(encoding)
        else: return output
    return p.returncode

Comments

0

I was having a similar problem using a Windows machine and found this was the easiest way of fixing my problem

import subprocess
cmd='gdalwarp -overwrite -s_srs EPSG:4283 -t_srs EPSG:28350 -of AAIGrid XXXXX.tif XXXXX.asc'
subprocess.run(cmd,shell=True)

change cmd for whatever you want to run. I iterated this > 1000 times and without the command prompt window appearing, I was able to save a lot of time.

Comments

0

When using Python 3.5+ you can also use the newer subprocess.run(...) method and pass the capture_output=True option:

import subprocess
subprocess.run(['/etc/init.d/apache2', 'restart'], capture_output=True)

See the examples in the using-the-subprocess-module section in the docs in case you actually want to capture the output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.