自定义日志文件被自动覆盖

自定义日志文件被自动覆盖

我有一个 Linux Bash 脚本,用于将文本附加到文件(通过使用>>)。此文件在启动时运行,并且每秒循环运行一次。如果计算机重新启动,它将从上次中断的地方继续。我的脚本在几个小时内按预期运行。但是,我注意到有时我正在记录的文件会被覆盖。我不确定为什么。我使用的是>>,而不是>

兴趣点:

  1. 我正在登录 nvidia jetson TX1。

  2. 我曾经tx_masterlogger.sh作为服务运行的进程是:

    • sudo chmod a+x tx1_masterlogger.sh
    • sudo chmod 777 tx1_masterlogger.sh
    • 将 tx1_masterlogger.sh 移至 /etc/init.d
    • sudo update-rc.d tx1_masterlogger.sh defaults

这是我的代码:

#!/bin/bash
#Boot Logger - to log boot times.
./home/ubuntu/logs/scripts/tx1_bootlogger.sh &

#Dmesg Logger 
sudo dmesg -T --follow >> "/home/ubuntu/logs/dmesg/dmesg_$(date 
+"%Y_%m_%d").log" &

   while :; do

      #Powersupply Logger
      ./home/ubuntu/logs/scripts/tx1_powersupplylogger.sh &

sleep 1
done

此外,这是tx1_powersupply.sh文件:

#!/bin/bash
#Channels
  channel0='/sys/devices/platform/7000c400.i2c/i2c-1/1- 
0040/iio:device0/'

#Write headers if file is newly created
if [ ! -f "/home/ubuntu/logs/powersupply/powersupply_$(date +"%Y_%m_%d").csv" ]; then

echo 'UTC Time,'\
'crit_current_limit_0,'\
'in_current0_input,'\
'in_current0_trigger_input,'\
'in_power0_input,'\
'in_power0_trigger_input,'\
'in_voltage0_input,'\
'rail_name_0,'\
'ui_input_0,'\
'warn_current_limit_0,'\
'crit_current_limit_1,'\
'in_current1_input,'\
'in_current1_trigger_input,'\
'in_power1_input,'\
'in_power1_trigger_input,'\
'in_voltage1_input,'\
'rail_name_1,'\
'ui_input_1,'\
'warn_current_limit_1,'\
'crit_current_limit_2,'\
'in_current2_input,'\
'in_current2_trigger_input,'\
'in_power2_input,'\
'in_power2_trigger_input,'\
'in_voltage2_input,'\
'rail_name_2,'\
'ui_input_2,'\
'warn_current_limit_2' >> 
 "/home/ubuntu/logs/powersupply/powersupply_$(date +"%Y_%m_%d").csv"

fi

#VDD_IN PARAMTERS
path_crit_current_limit_0=$channel0'crit_current_limit_0'
path_in_current0_input=$channel0'in_current0_input'
path_in_current0_trigger_input=$channel0'in_current0_trigger_input'
path_in_power0_input=$channel0'in_power0_input'
path_in_power0_trigger_input=$channel0'in_power0_trigger_input'
path_in_voltage0_input=$channel0'in_voltage0_input'
path_rail_name_0=$channel0'rail_name_0'
path_ui_input_0=$channel0'ui_input_0'
path_warn_current_limit_0=$channel0'warn_current_limit_0'

crit_current_limit_0=$(cat $path_crit_current_limit_0)
in_current0_input=$(cat $path_in_current0_input)
in_current0_trigger_input=$(cat $path_in_current0_trigger_input)
in_power0_input=$(cat $path_in_power0_input)
in_power0_trigger_input=$(cat $path_in_power0_trigger_input)
in_voltage0_input=$(cat $path_in_voltage0_input)
rail_name_0=$(cat $path_rail_name_0)
ui_input_0=$(cat $path_ui_input_0)
warn_current_limit_0=$(cat $path_warn_current_limit_0)

#VDD_GPU PARAMETERS
path_crit_current_limit_1=$channel0'crit_current_limit_1'
path_in_current1_input=$channel0'in_current1_input'
path_in_current1_trigger_input=$channel0'in_current1_trigger_input'
path_in_power1_input=$channel0'in_power1_input'
path_in_power1_trigger_input=$channel0'in_power1_trigger_input'
path_in_voltage1_input=$channel0'in_voltage1_input'
path_rail_name_1=$channel0'rail_name_1'
path_ui_input_1=$channel0'ui_input_1'
path_warn_current_limit_1=$channel0'warn_current_limit_1'

crit_current_limit_1=$(cat $path_crit_current_limit_1)
in_current1_input=$(cat $path_in_current1_input)
in_current1_trigger_input=$(cat $path_in_current1_trigger_input)
in_power1_input=$(cat $path_in_power1_input)
in_power1_trigger_input=$(cat $path_in_power1_trigger_input)
in_voltage1_input=$(cat $path_in_voltage1_input)
rail_name_1=$(cat $path_rail_name_1)
ui_input_1=$(cat $path_ui_input_1)
warn_current_limit_1=$(cat $path_warn_current_limit_1)

#VDD_CPU PARAMETERS
path_crit_current_limit_2=$channel0'crit_current_limit_2'
path_in_current2_input=$channel0'in_current2_input'
path_in_current2_trigger_input=$channel0'in_current2_trigger_input'
path_in_power2_input=$channel0'in_power2_input'
path_in_power2_trigger_input=$channel0'in_power2_trigger_input'
path_in_voltage2_input=$channel0'in_voltage2_input'
path_rail_name_2=$channel0'rail_name_2'
path_ui_input_2=$channel0'ui_input_2'
path_warn_current_limit_2=$channel0'warn_current_limit_2'

crit_current_limit_2=$(cat $path_crit_current_limit_2)
in_current2_input=$(cat $path_in_current2_input)
in_current2_trigger_input=$(cat $path_in_current2_trigger_input)
in_power2_input=$(cat $path_in_power2_input)
in_power2_trigger_input=$(cat $path_in_power2_trigger_input)
in_voltage2_input=$(cat $path_in_voltage2_input)
rail_name_2=$(cat $path_rail_name_2)
ui_input_2=$(cat $path_ui_input_2)
warn_current_limit_2=$(cat $path_warn_current_limit_2)

#Write to file
echo "`date -u`,"\
${crit_current_limit_0}','\
${in_current0_input}','\
${in_current0_trigger_input}','\
${in_power0_input}','\
${in_power0_trigger_input}','\
${in_voltage0_input}','\
${rail_name_0}','\
${ui_input_0}','\
${warn_current_limit_0}','\
${crit_current_limit_1}','\
${in_current1_input}','\
${in_current1_trigger_input}','\
${in_power1_input}','\
${in_power1_trigger_input}','\
${in_voltage1_input}','\
${rail_name_1}','\
${ui_input_1}','\
${warn_current_limit_1}','\
${crit_current_limit_2}','\
${in_current2_input}','\
${in_current2_trigger_input}','\
${in_power2_input}','\
${in_power2_trigger_input}','\
${in_voltage2_input}','\
${rail_name_2}','\
${ui_input_2}','\
${warn_current_limit_2} >> 
"/home/ubuntu/logs/powersupply/powersupply_$(date +"%Y_%m_%d").csv"

答案1

为什么不使用 journalctl?

使用 systemd 是最好的选择。https://manpages.debian.org/stretch/systemd/journalctl.1.en.html

相关内容