1

我有一个具有原始输入命令的 python 脚本,但我想在用户输入 raw_input 部分后在后台运行它。我遇到的问题是,如果我尝试使用 & 在后台运行脚本,原始输入会作为 linux 命令弹出,并且 python 脚本无法识别它。

有小费吗?

4

2 回答 2

1

您可以使用fork创建子进程然后退出父进程。

#!/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')
于 2013-10-02T18:08:09.303 回答
1

您可能希望在前台运行脚本,但os.fork()在用户输入值后调用。

于 2013-10-02T18:08:16.723 回答