简体   繁体   English

无法在后台运行python脚本

[英]Can not run python script in background

I wrote a python script. 我写了一个python脚本。 When I run it directly(like below) it works fine. 当我直接运行它(如下所示)时,它可以正常工作。

python check_C1_criteria_file.py > test.out

But when I run it in background(like below) it neither shows any result nor error. 但是,当我在后台运行它(如下所示)时,它既不显示任何结果,也不显示错误。

python check_C1_criteria_file.py > test.out &

or 要么

nohup python check_C1_criteria_file.py &

What can go wrong? 有什么问题吗? Can anyone help me with this? 谁能帮我这个?

Update : 更新

The main part of the script is as follow: 该脚本的主要部分如下:

 blastOutput_file=sys.argv[1];
 lengthFile = sys.argv[2];
 with open(blastOutput_file, 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter='\t')
    sys.stdout.write('#query_id'+'\t'+'Mapping_Id'+'\t'+'Description'+'\n');
    for row in reader:
        tid=row[0];
        subid=row[1];
        mapid=getMapping_id(subid);
        idDes = search_id(lengthFile, mapid);
        if idDes is not None:
            sys.stdout.write(tid+'\t'+str(mapid)+'\t'+str(idDes)+'\n');

Am I missing something? 我想念什么吗?

Is your script doing any sort of terminal handling? 您的脚本在进行任何终端处理吗? Does it do any I/O other than simple sys.stdout.write() or calls to print (Python2.x) or print() (Python3.x)? 除了简单的sys.stdout.write()或调用print (Python2.x)或print() (Python3.x)之外,它还执行其他任何I / O吗? Is it performing any input() or raw_input() or sys.stdin.read() operations? 它是否执行任何input()raw_input()sys.stdin.read()操作? Is it Python 2 or 3? 是Python 2还是3?

Roughly speaking the only sorts of things which differ when running a command in the background vs. in the foreground are those related to any calls it makes to your terminal. 粗略地说,在后台运行命令与在前台运行命令时,唯一不同的地方就是那些与您对终端的调用有关的东西。 A process in the background attempting to access your terminal may be put to sleep until its brought back into the foreground. 在后台尝试访问您的终端的过程可能会进入休眠状态,直到它回到前台为止。 Normal writes to stdout will not block ... but any calls to curses functions, even some of the termio stuff in getpass() might set the terminal into a mode that will block on attempted terminal writes. 正常写入stdout不会阻止...但是任何对curses函数的调用,甚至getpass()一些termio内容都可能会将终端设置为会在尝试进行终端写入时阻止的模式。

You can try 你可以试试

nohup python check_C1_criteria_file.py >test.out 2>&1 &

You had better examine that this program terminated normally. 您最好检查一下该程序是否正常终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM