1. 程式人生 > >ubuntu下python跑任務輸出到檔案中遇到的一些問題(輸出重定向)

ubuntu下python跑任務輸出到檔案中遇到的一些問題(輸出重定向)

之前主要是參考https://www.cnblogs.com/chason95/articles/9760291.html 一般使用

python test.py > ./log.txt
或
python test.py | tee ./log.txt

然後就會快取很多輸出後才能在螢幕或log中查到,就很難受。

後來領導給我了一條命令

srun --partition=GTX1080 --mpi=pmi2 --gres=gpu:1 -n1 --ntasks-per-node=1 --job-name=TEST --kill-on-bad-exit=1 python -u  test.py > log.txt 2>&1 &

去掉srun部分,大概是

python -u  test.py > log.txt 2>&1 &

這是啥玩意兒?(ubuntu都是用到了才學的,所以猛地一看有點懵逼)

但是這樣就可以在log中實時看到輸出資訊了(tail -f log.txt)

那麼這些亂七八糟的引數是啥意思呢?

首先,python -u

-u     : force the binary I/O layers of stdout and stderr to be unbuffered;
         stdin is always buffered; text I/O layer will be line-buffered;
         also PYTHONUNBUFFERED=x

原來如此!卡我的設定是這個!

然後>,就是簡單的重定向。

2>&1,這個又是啥呢?

There are two forms of redirection the standard output and standard error into standard output

2>&1就是其中一個,另一個是&>,那他們有什麼區別呢?

Only >& works in csh or tcsh

In ksh only 2>&1 works.

dash use >file 2>&1 redirection only

csh,tcsh,ksh,dash是啥參考https://blog.csdn.net/LEON1741/article/details/77931460

最後的&: the ampersand & at the end makes a command run in the background。這樣這條命令就可以在後臺跑不用佔這一個shell了。(用screen可以達到同樣的效果,不過沒這個方便。)

做個小筆記,就醬。

參考資料:

http://www.eetop.cn/blog/html/03/6503-25123.html

https://blog.csdn.net/LEON1741/article/details/77931460

https://askubuntu.com/questions/635065/what-is-the-differences-between-and-21

https://askubuntu.com/questions/833833/what-does-command-do/833848