Python—后台運行(nohup 、&、 2>&1詳解)


一、腳本文件(test.py)

# -*- coding: UTF-8 -*-
import time

print("hello"," python")
os.getcwd()      # 獲得當前工作目錄
os.chdir(path)   # 改變當前工作路徑

while True:
    print("start to print %s" % "python")   
    time.sleep(2)

二、運行腳本

[root@localhost ~]# python test.py >my.log
[root@localhost ~]# python test.py >my.log 2>&1
[root@localhost ~]# python test.py 1>my.log                          # 等同於第一條,只是1可以省略不寫
[root@localhost ~]# python test.py 1>my.log 2>&1                     # 等同於第二條,只是1可以省略不寫
[root@localhost ~]# python test.py 1>my.log 2>error.log              # 等同於第二條,只是把錯誤輸出到error.log文件了


[root@localhost ~]# python test.py >>my.log                            
[root@localhost ~]# python test.py >>my.log 2>&1
[root@localhost ~]# python test.py 1>>my.log                         # 等同於第一條,只是1可以省略不寫
[root@localhost ~]# python test.py 1>>my.log 2>&1                    # 等同於第二條,只是1可以省略不寫
[root@localhost ~]# python test.py 1>>my.log 2>>error.log            # 等同於第二條,只是把錯誤輸出到error.log文件了

三、2>&1

  • 0 表示stdin標准輸入,用戶鍵盤輸入的內容
  • 1 表示stdout標准輸出,輸出到顯示屏的內容
  • 2 表示stderr標准錯誤,報錯內容

2>&1是一個整體,>左右不能有空格,即將錯誤內容重定向輸入到標准輸出中去。

2>&1中的&是為了區別文件1和1(標准輸出),假如為2>1,就成了將錯誤內容輸出到文件1中,而不是標准輸出中。

四、nohup命令

https://blog.csdn.net/suyues/article/details/100046978

https://blog.csdn.net/weixin_42840933/article/details/85780125


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM