Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As per the bash reference, we can give a non-blocking command by ending it with an ampersand(&) character. However, when I try the below command its not working:

python -m SimpleHTTPServer 8080&

The reason I intend to do so is that I want to start the built-in python webserver as part of another larger program which is also written in python. Can you plz advice how do I issue this command in a non-blocking/daemon mode? I even tried to execute this command using "subprocess.Popen()" in python which is also supposed to create a non-blocking process, but even that is not working:-(..

EDIT: Here is the portion of my python code that starts the webserver (Adding 'nohup' seems to do the trick):

pid_webserver = execute("nohup python -m SimpleHTTPServer 8080 &",wait=False,shellexec= True)
#pid_webserver = execute("python -m SimpleHTTPServer 8080 &",wait=False,shellexec= True)

def execute(command,errorstring='', wait = True, shellexec = True):
        print 'command=' + command
        p=subprocess.Popen("gksu '" + command + "'", shell=shellexec,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        if wait:
            return result
            print 'not waiting'
            return p
    except subprocess.CalledProcessError as e:
        print 'error occured:' + errorstring
        return errorstring
share|improve this question

closed as unclear what you're asking by Gilles, Evan Teitelman, slm, jasonwryan, manatwork Jul 17 '13 at 7:49

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Please clarify: What do you mean by "not working"? Exactly what happens, and how does it differ from what you expect? – rici Jul 16 '13 at 15:58
As rici states, what exactly do you mean by "not working"? Works like a charm for me – iruvar Jul 16 '13 at 16:04
Is it because you have no space between the & and the end of your command? – jw013 Jul 16 '13 at 16:21
What I mean by "not working" is that it is not doing its stated purpose of creating a non-blocking process. When I issue the above command, it still blocks though its terminated by "&". – Prahlad Yeri Jul 16 '13 at 16:21
@jw013 - I tried with the space, still same results.. :-(( – Prahlad Yeri Jul 16 '13 at 16:22

The & works. But I think what your are looking for is nohup python -m SimpleHTTPServer &.

But I thinks you should also look at http://docs.python.org/2/library/simplehttpserver.html.

share|improve this answer
If it works, then why does my command blocks? It should just start the server and exit, right? But it doesn't. – Prahlad Yeri Jul 16 '13 at 16:23
@PrahladYeri can you post you subprocess.Popen line in your program with all the parameters. – Manula Waidyanatha Jul 16 '13 at 17:06
Thanks for the nohup idea. Its the only thing that runs as non-blocking. Otherwise, even subprocess.Popen() is blocking. – Prahlad Yeri Jul 16 '13 at 17:10
I've also updated the question with the subprocess.Popen code. – Prahlad Yeri Jul 16 '13 at 17:13
Need one more help. How do I issue "cd" and the python server at once. I tried this but its not working : "cd /mypath | nohup python -m SimpleHTTPServer 8080 &" (current directory does not change) – Prahlad Yeri Jul 16 '13 at 17:38

'&' will not work here ... It is a bash operator, in your script you are not using bash to execute the command, you are executing it from python script - that is why it will never work.

try :

import os
os.system("python -m SimpleHTTPServer 8080 &")
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.