Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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):
    try:
        print 'command=' + command
        p=subprocess.Popen("gksu '" + command + "'", shell=shellexec,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        if wait:
            p.wait()
            result=get_stdout(p)
            return result
        else:
            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, paraxor, 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.

4  
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 –  1_CR 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
add comment

2 Answers 2

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
show 1 more comment

'&' 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
add comment

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