简体   繁体   中英

Can not run python script in background

I wrote a python script. 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)? Is it performing any input() or raw_input() or sys.stdin.read() operations? Is it Python 2 or 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.

You can try

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

You had better examine that this program terminated normally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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