1

May be the question is obscure. The example code below for clarify.

# file: sock.py

password = getpass.getpass("Password: ")


def run_socket_server():
    with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
        # code...


if __name__ == "__main__":
    run_socket_server()

Run the script

python sock.py
Password:   # enter password

After this the process stay running not in background, blocking user input in terminal. So the question is how to continue to executing the python process in background to allow user input in terminal?

vczm
  • 574
  • 6
  • 14

2 Answers2

3

This solution assumes that you want to use your Python program unchanged.

As your program asks for a password it must run in the foreground first. Running it as

python sock.py &

would stop it by a signal SIGTTIN when it wants to read the password. That's why start it in the foreground as you did.

If after reading the password it does not require any more terminal input, you can stop it after entering the password by pressing CTRL+Z and then send it to the background by executing bg.

(If for some reason you need to get the job into the foreground again, e.g. to provide input, execute fg.)

Bodo
  • 9,287
  • 1
  • 13
  • 29
0

You want to access terminal while python code is running in the background, you may need to add ampersand at the end

python sock.py &
  • 1
    The background process will be stopped by a signal `SIGTTIN` when it tries to read the password from the terminal. – Bodo Mar 14 '19 at 16:01