nohup
命令用于在后臺運行程序,即使關閉終端或斷開連接,程序也會繼續運行。默認情況下,nohup
會將程序的輸出重定向到名為 nohup.out
的文件中。如果你想自定義日志輸出格式,可以使用 shell 腳本或其他工具來實現。
以下是一個簡單的示例,展示了如何使用 shell 腳本自定義 nohup
日志輸出格式:
custom_nohup.sh
的 shell 腳本文件:#!/bin/bash
# 獲取當前日期和時間
current_date=$(date '+%Y-%m-%d %H:%M:%S')
# 獲取要運行的命令
command="$@"
# 將命令的輸出重定向到自定義格式的日志文件
nohup sh -c "echo '[$current_date] Starting command: $command'; $command >> custom_nohup.log 2>&1" &
chmod +x custom_nohup.sh
custom_nohup.sh
腳本運行你的程序。例如,如果你想運行名為 my_program
的程序,可以這樣做:./custom_nohup.sh my_program
這將在后臺運行 my_program
,并將輸出重定向到名為 custom_nohup.log
的文件中。日志文件的每條記錄將以自定義的日期和時間格式開頭。
你可以根據需要修改 custom_nohup.sh
腳本,以實現所需的日志格式和其他功能。