python - using os.system for multiple line commands -


i trying run shell code python file submit python file computing cluster. shell code follows:

#bsub -j proc[1] #bsub -e ~/logs/proc.%i.%j.err #bsub -o ~/logs/proc.%i.%j.out #bsub -r "span[hosts=1]" #bsub -n 1 python main.py 

but when run python following can't work:

from os import system system('bsub -n 1 < #bsub -j proc[1];#bsub -e ~/logs/proc.%i.%j.err;#bsub -o ~/logs/proc.%i.%j.out;#bsub -r "span[hosts=1]";#bsub -n 1;python main.py') 

is there i'm doing wrong here?

if understand correctly, #bsub stuff text should fed bsub command input; bsub run locally, runs commands on compute node.

in case, can't do:

bsub -n 1 < #bsub -j proc[1];#bsub -e ~/logs/proc.%i.%j.err;#bsub -o ~/logs/proc.%i.%j.out;#bsub -r "span[hosts=1]";#bsub -n 1;python main.py 

that's interpreted shell "run bsub -n 1 , read file named oh crap comment started , don't have file read!"

you fix moar hackery (using echo or here strings taking further unnecessary dependencies on shell execution). if want feed stdin input, best solution use more powerful tool task, the subprocess module:

# open process (no shell wrapper) can feed stdin proc = subprocess.popen(['bsub', '-n', '1'], stdin=subprocess.pipe)  # feed command series needed stdin, wait process complete # per michael closson, can't use semi-colons, bsub requires newlines proc.communicate(b'''#bsub -j proc[1] #bsub -e ~/logs/proc.%i.%j.err #bsub -o ~/logs/proc.%i.%j.out #bsub -r "span[hosts=1]" #bsub -n 1 python main.py ''')  # assuming exit code meaningful, check here if proc.returncode != 0:     # handle failed process launch here 

this avoids shell launch entirely (removing issue needing deal comment characters @ all, along other issues handling shell metacharacters), , more explicit being run locally (bsub -n 1) , commands being run in bsub session (the stdin).


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -