腳本執行時同時捕獲標準輸出與錯誤輸出到同一日誌檔:
/root/test.sh >> test.log 2>&1
>>:追加標準輸出 (stdout, fd=1) 到 test.log。2>&1:將標準錯誤 (stderr, fd=2) 重定向至 fd 1 (即同一檔案)。順序很重要:2>&1 必須在 >> 之後,否則 stderr 仍指向終端。
/etc/rc.d/rc.local → /etc/profile。
若需在開機早期執行腳本並記錄日誌,置入 rc.local 並使用上述重定向。