3

I tried executing a server daemon with gnu screen from subprocess call but it didn't even start

subprocess.call(["screen", "-dmS test ./server"])

I was told that running screen requires terminal, hence the reason why I can't simply execute it with call. Can you show me some piece of codes to do this?

1 Answer 1

8

Try

subprocess.call( ["screen", "-d", "-m", "-S", "test", "./server"] )

You need to break the argument string into separate arguments, one per string.

Here's the relevant quote from the subprocess docs:

On UNIX, with shell=False (default): In this case, the Popen class uses os.execvp() to execute the child program. args should normally be a sequence. A string will be treated as a sequence with the string as the only item (the program to execute).

On UNIX, with shell=True: If args is a string, it specifies the command string to execute through the shell. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional shell arguments.

So by default, the arguments are used exactly as you give them; it doesn't try to parse a string into multiple arguments. If you set shell to true, you could try the following:

subprocess.call("screen -dmS test ./server", shell=True)

and the string would be parsed exactly like a command line.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That works but it's strange that I need to break all the args that way. Thought I could put all the arguments together
Answer updated to (attempt to) explain why breaking up the args is necessary.

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.