在Debian系统中,nohup
命令用于在后台运行程序,即使关闭终端或断开连接,程序也会继续运行。默认情况下,nohup
会将输出重定向到名为nohup.out
的文件中。如果你想配置nohup
的日志级别,可以通过以下几种方法实现:
nohup
和&>
重定向你可以将标准输出和标准错误都重定向到一个日志文件,并使用tee
命令来记录不同级别的日志。
nohup your_command > log_output.log 2>&1 | tee -a log_output.log &
syslog
如果你希望将日志发送到系统的日志服务(如syslog
),可以使用logger
命令。
nohup your_command &>/dev/log &
或者使用syslog-ng
或rsyslog
配置文件来指定日志级别。
syslog-ng
编辑/etc/syslog-ng/syslog-ng.conf
文件,添加如下内容:
destination d_your_command {
file("/var/log/your_command.log");
};
filter f_your_command {
program("your_command");
};
log {
source(s_src);
destination(d_your_command);
filter(f_your_command);
};
然后重启syslog-ng
服务:
sudo systemctl restart syslog-ng
rsyslog
编辑/etc/rsyslog.conf
或/etc/rsyslog.d/50-default.conf
文件,添加如下内容:
if $programname == 'your_command' then /var/log/your_command.log
& stop
然后重启rsyslog
服务:
sudo systemctl restart rsyslog
你可以编写一个自定义脚本来捕获和记录不同级别的日志。
#!/bin/bash
LOG_LEVEL=$1
shift
COMMAND="$@"
exec &> >(tee -a /var/log/your_command.log)
echo "[$(date)] Starting command with log level $LOG_LEVEL"
$COMMAND
echo "[$(date)] Command finished"
保存为run_with_log_level.sh
,然后使用如下方式运行:
nohup ./run_with_log_level.sh info your_command &
systemd
服务如果你希望通过systemd
管理服务并配置日志级别,可以创建一个systemd
服务单元文件。
sudo nano /etc/systemd/system/your_command.service
[Unit]
Description=Your Command Service
[Service]
ExecStart=/path/to/your_command
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=your_command
Restart=always
[Install]
WantedBy=multi-user.target
systemd
配置:sudo systemctl daemon-reload
sudo systemctl start your_command
sudo systemctl enable your_command
通过这些方法,你可以在Debian系统中灵活地配置nohup
的日志级别。选择适合你需求的方法进行配置即可。