Hello All,
i’m starting facing linux and i’d like to ask you something.
I have 2 log files and i would like to catch all the info i need from all of them and have a single output.
Every time i’m using the full command from the root like :
tac /folder/application/log/log1.txt | head - 1 (because i want to have an overview of the last ten lines)
if i found as the last line the timestamp of today then i’m checking another log
tac /folder/application/log/log2.txt | grep result | head -1 (because i want to grep the last result of the operation)
if not
i’m going to check where the application is locked (the application create *.lck file in three different directories according to the stage of the process), let say :
$ cat log2.txt
Mar 30 12:23:49 callipso hp-upgrade: hp-upgrade[4434]: error: Either Internet is not working or Wget is not installed.
Mar 30 15:28:04 callipso hp-upgrade: hp-upgrade[4423]: error: Either Internet is not working or Wget is not installed.
Mar 30 15:28:25 callipso mtp-probe: checking bus 3, device 4: "/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1.2"
Mar 30 15:28:25 callipso mtp-probe: bus: 3, device: 4 was not an MTP device
Mar 30 22:22:11 callipso mtp-probe: checking bus 2, device 2: "/sys/devices/pci0000:00/0000:00:1c.6/0000:0e:00.0/usb2/2-1"
Mar 30 22:22:11 callipso mtp-probe: bus: 2, device: 2 was not an MTP device
First we are going to check whether log1.txt contains today’s logs. If yes we will check log2.txt. If no output found script will exit.
Check log2.txt. If no output found check lock file. If there is a todays log in log2.txt we will print last 10 lines and exit
If lock file exists print LOCKED else print UNCLOKED
Please note that to match your environment I would need to see a sample of your log files as the timestamp may be different. However, the below script should give you a good start.
#!/bin/bash
TODAY=`echo $(date +"%Y-%m-%d")`
grep -q "`date --date="today" +%b\ %e`" log1.txt
if [ $? -eq 0 ]; then
grep -q "`date --date="today" +%b\ %e`" log2.txt
if [ $? -eq 0 ]; then
tail log2.txt
else
if [ -f opt/logs/preprocess/$TODAY/app.lck ]; then
echo LOCKED
else
echo UNLOCKED
fi
fi
fi
OUTPUT:
$ bash check_log.sh
LOCKED
Hope this helps to get you started. Otherwise it would be great to have some sample files of your logs and lock files so we can construct a bash script to match your exact environment.