
我想在系統啟動時使用自動啟動中的快捷方式/連結 (.lnk) 以靜默方式運行 Python 3 腳本。
只有在某處管道 stdout 和 stderr 時,Python 的 pythonw.exe 才會作為無視窗進程與我的腳本一起運行pythonw script.pyw >nul 2>&1
。這在 CMD 中確實有效。
當作為 Windows 連結目標給出時,它不起作用。這會自動擴展為c:\Python34\pythonw.exe script.pyw >nul 2>&1
.指令周圍的雙引號會自動刪除。 pythonw.exe 進程僅在執行連結時短暫運行。
答案1
我可以使用它
C:\Windows\system32\CMD.exe /C start /B pythonw.exe script.pyw >nul 2>&1
運行時,會短暫顯示 CMD 視窗。看來 CMD 管道運算子不是由 Windows 連結「機制」解釋的,而是作為參數/選項傳送到程式。透過實際執行帶有 /C 的 CMD 以傳遞 START 命令以將 pythonw 作為單獨的進程運行來解決此問題。
延伸閱讀:
- http://ss64.com/nt/cmd.html- 啟動新的 CMD shell 並(可選)運行命令/可執行程式。
- http://ss64.com/nt/start.html- 啟動程式、命令或批次腳本(在新視窗中開啟。)
- http://ss64.com/nt/syntax-redirection.html