I'm trying to get some files with zero size and insert it with a text. I create a simple script and put it in the crontab every 15 minutes.
I use find
after read this question i found it before How to find all Zero bytes files in directory including subdirectories.
Here the script.
#!/usr/bin/bash
statPath=/opt/player/for_test/testing
date=`date +%Y%m%d` #Untuk mengambil tanggal (YYYY-MM-DD)
cm=$(date +%M)
host=`hostname`
for n in `find $statPath -size 0`
do
if [ $cm -ge 0 ] && [ $cm -le 15 ];then
echo $date"00""|"$host"|"0x00"|"0 >> $n
elif [ $cm -ge 16 ] && [ $cm -le 30 ];then
echo $date"15""|"$host"|"0x00"|"0 >> $n
elif [ $cm -ge 31 ] && [ $cm -le 45 ];then
echo $date"30""|"$host"|"0x00"|"0 >> $n
elif [ $cm -ge 46 ] && [ $cm -le 59 ];then
echo $date"45""|"$host"|"0x00"|"0 >> $n
fi
done
File in testing
directory before running the script
-rw-r--r-- 1 root root 0 Jun 13 13:46 Server01_2016061313_45.log
-rw-r--r-- 1 root root 0 Jun 13 14:01 Server01_2016061314_00.log
-rw-r--r-- 1 root root 0 Jun 13 14:16 Server01_2016061314_15.log
-rw-r--r-- 1 root root 0 Jun 13 14:31 Server01_2016061314_30.log
-rw-r--r-- 1 root root 0 Jun 13 14:46 Server01_2016061314_45.log
-rw-r--r-- 1 root root 0 Jun 13 15:01 Server01_2016061315_00.log
File in testing
directory after running the script.
-rw-r--r-- 1 root root 28 Jun 13 13:46 Server01_2016061313_45.log
-rw-r--r-- 1 root root 28 Jun 13 14:01 Server01_2016061314_00.log
-rw-r--r-- 1 root root 28 Jun 13 14:16 Server01_2016061314_15.log
-rw-r--r-- 1 root root 28 Jun 13 14:31 Server01_2016061314_30.log
-rw-r--r-- 1 root root 28 Jun 13 14:46 Server01_2016061314_45.log
-rw-r--r-- 1 root root 28 Jun 13 15:01 Server01_2016061315_00.log
The result that I got is all the file is changed with the same format. I run the script at 3:34 PM.
root@Server01:/opt/player/for_test/testing# more Server01_2016061313_45.log
2016061330|Server01|0x00|0
root@Server01:/opt/player/for_test/testing#
root@Server01:/opt/player/for_test/testing# more Server01_2016061314_15.log
2016061330|Server01|0x00|0
Expected result
For log with "00" minute, the result is 2016061300|Server01|0x00|0
For log with "15" minute, the result is 2016061315|Server01|0x00|0
For log with "30" minute, the result is 2016061330|Server01|0x00|0
For log with "45" minute, the result is 2016061345|Server01|0x00|0
Is there any easy way to do this? I kinda stuck in here.