0

I am trying to create a logrotate script for a running nohup output ( without any line breakages - tried logrotate package in system and observed several log lines are getting missed while rotating the continuously generating log file). Here's the steps i followed,

  • Run the below script in background
#!/bin/bash
log_split_pipe="/tmp/log_split_pipe"
log_rename_interval_in_sec=60
log_file="/home/application/Logs/appname.log"
semaphore="/home/application/Logs/appname.log.pause"
write_log()
{
 while read line
 do
  while [ -f $semaphore ]
  do
   sleep 1
  done
 echo "$line" >> $log_file
 done < $log_split_pipe
}

write_log &
log_start_time=$(date +%s)
while true
do
 tim_diff=$(expr $(date +%s) - $log_start_time)
 if [ $tim_diff -ge $log_rename_interval_in_sec ];then
  touch $semaphore
  mv /home/application/Logs/appname.log /home/application/Logs/appname-$(date +%Y'_'%m'_'%d'_'%H'_'%M).log
  rm -rf $semaphore
  log_start_time=$(date +%s)
 fi
done
  • mkfifo /tmp/log_split_pipe
  • run application as

nohup ./application 2>&1 1 >/tmp/log_split_pipe &

Here the problem is, to the log file /home/application/Logs/appname.log I am getting junk texts instead of the proper logs written by the process.

Can anyone help on the problem with the logic and to rectify?

Riyas Siddikk
  • 186
  • 2
  • 11
  • "Junk"? Could you elaborate? "instead of" - so none of the expected output? Is it possible your app is receiving that spaced-apart `1` as a behavior-changing relevant argument instead of part of the output redirection? Have you checked for other processes writing to the same location? – Paul Hodges Oct 18 '17 at 14:01
  • Yes, what i meant is the proper application output log is not writing to the log file if i follow the above step. If i directly give the nohup out to a regular file the logs are fine. and also, i have cross checked the same case with other java based processes too, where the issue is same. So now, not sure if the issue may occur for all the `stdout` logs from any process which continuously write to a name-piped file. As i mentioned above, I was trying to create a log rotate script without any line miss for high log generating files. – Riyas Siddikk Oct 18 '17 at 14:59
  • @PaulHodges : can my explanation help you to find me a right solution, pls? – Riyas Siddikk Oct 23 '17 at 05:02

0 Answers0