Question

I have a python script that has a raw input command, but I would like to run it in the background after the user inputs the raw_input part. The problem I have is if I try running the script in the background using &, the raw input pops up as a linux command and the python script doesn't recognize it.

Any tips?

Was it helpful?

Solution

You can use fork to create a child process and then exit the parent.

#!/usr/bin/env python

import os
import sys
import time

_ = raw_input('Enter the the secret code: ')
if os.fork(): # returns 0 in the child, pid of the child in the parent
    sys.exit()

time.sleep(2)
print('All good things must come to an end')

OTHER TIPS

You probably want to run the script in the foreground, but then call os.fork() after the user has input the value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top