bashスクリプトでexecを使うと、シェルが実行したコマンドに置き換えられる。
Pythonのスクリプトをシェルスクリプトから実行することが結構あって、プロセスの監視云々とか考えるとexec使ったらいいんじゃないとかどっかで聞いた気がする。
execを使わない場合
以下のようなPythonスクリプトをbashスクリプト経由で実行してみる。
main.py
# coding: utf-8 import time def main(): while True: time.sleep(1) if __name__ == '__main__': main()
run
#!/bin/bash
python main.py
実行結果
$ ./run ^Z [1]+ Stopped ./run $ ps aux (中略) tokibito 30303 0.0 0.1 12312 1160 pts/1 T 23:42 0:00 /bin/bash ./run tokibito 30304 0.5 0.5 32172 5304 pts/1 T 23:42 0:00 python main.py
execを使う場合
run
#!/bin/bash exec python main.py
実行結果
$ ./run ^Z [1]+ Stopped ./run $ ps aux (中略) tokibito 30348 0.1 0.5 32172 5308 pts/1 T 23:47 0:00 python main.py
execを使わない場合は2プロセスになってしまうけど、execを使うと1プロセスで済む。