Shell Script to read two different logs and output via Echo


#1

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 :

opt/logs/preprocess/%todaydate%/.lck
opt/logs/process/%todaydate%/
.lck
opt/logs/postrocess/%todaydate%/*.lck

Now i’d like to have a clue where to start because it will be really helpfull to do it quickly via script.


#2

Hi,

Based on your information I have created a following testing environment:

$ ls -R
.:
check_log.sh  log1.txt  log2.txt  opt

./opt:
logs

./opt/logs:
preprocess

./opt/logs/preprocess:
2014-04-01

./opt/logs/preprocess/2014-04-01:
app.lck

The log files have a following content:

$ cat log1.txt 
Apr  1 07:36:46 callipso anacron[25166]: Job `cron.daily' terminated (exit status: 1) (mailing output)
Apr  1 07:36:46 callipso anacron[25166]: Normal exit (1 job run)
Apr  1 07:39:01 callipso /USR/SBIN/CRON[26919]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -ignore_readdir_race -cmin +$(/usr/lib/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete)
$ 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.

Lubos


#3

#4