List of Running Jobs In Last 4 Hours

Hi Experts,
Please help me in this.
I am trying this code on AIX 5.3.
I need list of jobs that executed in last 4 hours.
I have a schedule on this script - cron executes it and sends mail to me for every 2 hours.
I have a Job time and have around 100 jobs those execute daily.
What all i need is when my cron executes below scripts it should capture list of jobs executed in last 4 hours.

Code:

#!/usr/bin/sh
#Scripted by Raju B on Aug062011#
#Datastge Jobs monitoring script
cd /QIS2FTP/scripts/Scripts
#Removing Last Run Jobstatus.txt file.
rm /QIS2FTP/scripts/Scripts/Jobstatus.txt
#Printing Output in Jobstatus.txt
Jobstatus=/QIS2FTP/scripts/Scripts/Jobstatus.txt
 echo "JobName,StartDateTime,EndDateTime,Status " >>$Jobstatus
 echo "------------------------------------------------------------" >>$Jobstatus
#set -x
# import my environment
 . /QIS2FTP/dsclientprod/home/my.env
#Variables declaration
#List of all jobs in DS QIS2 Project
DSJoblist=`dssearch -ljobs -matches -sub -oc QIS2 * |cut -f2 -d'\'|awk '{print $1}'`
Project=QIS2
SysDate=`date +%Y%m%d`
 
for i in $DSJoblist
do
   #DSJobStatus variable for collecting Job start and End time,Job duration and Job status
   DSJobStatus=`dsjob -report $Project $i 2> /dev/null | grep "Job" | cut -f2 -d'='`
   StartTime=`echo $DSJobStatus | cut -f1,2 -d' '`
   EndTime=`echo $DSJobStatus | cut -f3,4 -d' '`
   JobStat=`dsjob -report $Project $i | grep status | awk -F= ' { print $2; } ' | awk ' { print $1; } '`;
   JobName=`dssearch -ljobs -matches -sub -oc QIS2 $i`
   #Dateformat 20110805
   JobRunDate=`echo $StartTime |awk '{print $1}'|sed 's/-//'|sed 's/-//'`
   #Time in hours ex: 09
   JobTimeH=`echo $StartTime |awk '{print $2}'|cut -f1 -d':'`
 
   JobStTime=`echo "$JobTimeH"`
 
    #Time in hours ex: 09
   SystemHours=`date +%H`
   if [ "$SystemHours" -eq "01" ]
      then CurSysHrs=`echo "21"`
   fi
   if [ "$SystemHours" -eq "02" ]
      then CurSysHrs=`echo "22"`
   fi
   if [ "$SystemHours" -eq "03" ]
      then CurSysHrs=`echo "23"`
   fi
   if [ "$SystemHours" -eq "04" ]
      then CurSysHrs=`echo "00"`
   fi
   if [ "$SystemHours" -ge "05" ]
      then CurSysHrs=`echo "$SystemHours"+"-4" |bc`
   fi
   #Going back 4 hours for system time in a day
   #Static="-4"
   #Going 4 hour back from local time zone EST.
   #CurSysHrs=`TZ=EDT-4:00 date '+%H%M'`
   #echo "Print Current time before 4 hours $CurSysHrs"
     if [ "$SysDate" == "$JobRunDate" ] && [ "$JobStTime" -ge "$CurSysHrs" ] && [ "$JobStTime" -le "$SystemHours" ]
       then
             if [ "x1" == "x${JobStat}" ] || [ "x0" < "x${JobStat}" ] || [ "x2" != "x${JobStat}" ]
                then
                        echo "$JobName,$StartTime,$EndTime,$JobStat"  >>$Jobstatus
              fi
     fi
done
#set +

---------- Post updated at 12:36 PM ---------- Previous update was at 12:35 PM ----------

Please validate this code :
Code:

 
#!/usr/bin/sh
#Scripted by Raju B on Aug062011#
#Datastge Jobs monitoring script
cd /QIS2FTP/scripts/Scripts
#Removing Last Run Jobstatus.txt file.
rm /QIS2FTP/scripts/Scripts/Jobstatus.txt
#Printing Output in Jobstatus.txt
Jobstatus=/QIS2FTP/scripts/Scripts/Jobstatus.txt
 echo "JobName,StartDateTime,EndDateTime,Status " >>$Jobstatus
 echo "------------------------------------------------------------" >>$Jobstatus
#set -x
# import my environment
 . /QIS2FTP/dsclientprod/home/my.env
#Variables declaration
#List of all jobs in DS QIS2 Project
DSJoblist=`dssearch -ljobs -matches -sub -oc QIS2 * |cut -f2 -d'\'|awk '{print $1}'`
Project=QIS2
SysDate=`date +%Y%m%d`
for i in $DSJoblist
do
   #DSJobStatus variable for collecting Job start and End time,Job duration and Job status
   DSJobStatus=`dsjob -report $Project $i 2> /dev/null | grep "Job" | cut -f2 -d'='`
   StartTime=`echo $DSJobStatus | cut -f1,2 -d' '`
   EndTime=`echo $DSJobStatus | cut -f3,4 -d' '`
   JobStat=`dsjob -report $Project $i | grep status | awk -F= ' { print $2; } ' | awk ' { print $1; } '`;
   JobName=`dssearch -ljobs -matches -sub -oc QIS2 $i`
   #Dateformat 20110805
   JobRunDate=`echo $StartTime |awk '{print $1}'|sed 's/-//'|sed 's/-//'`
   #Time in hours ex: 09
   JobTimeH=`echo $StartTime |awk '{print $2}'|cut -f1 -d':'`
   JobStTime=`echo "$JobTimeH"`
   #Time in hours ex: 09
   SystemHours=`date +%H`
   if [ "$SystemHours" -ge "05" ]
      then CurSysHrs=`echo "$SystemHours"+"-4" |bc`
   fi
   #Going back 4 hours for system time in a day
   #Static="-4"
   #Going 4 hour back from local time zone EST.
   #CurSysHrs=`TZ=EDT-4:00 date '+%H%M'`
   #echo "Print Current time before 4 hours $CurSysHrs"
#Jobs Running After 08 PM and Before12 AM
     if [ "$SysDate" == "$JobRunDate" ] && [ "$SystemHours" -eq "0" ]
        then
             if [ "$JobStTime" -ge "20" ] || [ "$JobStTime" -le "0" ]
                then
                 if [ "x1" == "x${JobStat}" ] || [ "x0" < "x${JobStat}" ] || [ "x2" != "x${JobStat}" ]
                    then
                          echo "$JobName,$StartTime,$EndTime,$JobStat"  >>$Jobstatus
                 fi
             fi
     fi
#Jobs Running After 09 PM and Before 01 AM
     if [ "$SysDate" == "$JobRunDate" ] && [ "$SystemHours" -eq "01" ]
        then
             if [ "$JobStTime" -ge "21" ] || [ "$JobStTime" -le "1" ]
                then
                 if [ "x1" == "x${JobStat}" ] || [ "x0" < "x${JobStat}" ] || [ "x2" != "x${JobStat}" ]
                    then
                          echo "$JobName,$StartTime,$EndTime,$JobStat"  >>$Jobstatus
                 fi
             fi
     fi
#Jobs Running After 10 PM an Before 02 AM
     if [ "$SysDate" == "$JobRunDate" ] && [ "$SystemHours" -eq "02" ]
        then
             if [ "$JobStTime" -ge "22" ] || [ "$JobStTime" -le "2" ]
                then
                 if [ "x1" == "x${JobStat}" ] || [ "x0" < "x${JobStat}" ] || [ "x2" != "x${JobStat}" ]
                    then
                          echo "$JobName,$StartTime,$EndTime,$JobStat"  >>$Jobstatus
                 fi
             fi
     fi
#Jobs Running After 11 an Before 03 AM
     if [ "$SysDate" == "$JobRunDate" ] && [ "$SystemHours" -eq "03" ]
       then
             if [ "$JobStTime" -ge "23" ] || [ "$JobStTime" -le "3" ]
                then
                 if [ "x1" == "x${JobStat}" ] || [ "x0" < "x${JobStat}" ] || [ "x2" != "x${JobStat}" ]
                    then
                          echo "$JobName,$StartTime,$EndTime,$JobStat"  >>$Jobstatus
                 fi
             fi
     fi
#Jobs Running After 12 AM and Before04 AM
     if [ "$SysDate" == "$JobRunDate" ] && [ "$SystemHours" -eq "04" ]
        then
             if [ "$JobStTime" -ge "00" ] || [ "$JobStTime" -le "4" ]
                then
                 if [ "x1" == "x${JobStat}" ] || [ "x0" < "x${JobStat}" ] || [ "x2" != "x${JobStat}" ]
                    then
                          echo "$JobName,$StartTime,$EndTime,$JobStat"  >>$Jobstatus
                 fi
             fi
     fi
#Jobs Running atfter 05 AM
     if [ "$SysDate" == "$JobRunDate" ] && [ "$SystemHours" -ge "05" ]
        then
             if [ "$JobStTime" -ge "$CurSysHrs" ]                 then
                 if [ "x1" == "x${JobStat}" ] || [ "x0" < "x${JobStat}" ] || [ "x2" != "x${JobStat}" ]
                    then
                          echo "$JobName,$StartTime,$EndTime,$JobStat"  >>$Jobstatus
                 fi
             fi
     fi
 
done
#set +

What exactly isn't working?

#!/usr/bin/sh
#Scripted by Raju B on Aug062011#
#Datastge Jobs monitoring script
cd /QIS2FTP/scripts/Scripts
#Removing Last Run Jobstatus.txt file.
rm /QIS2FTP/scripts/Scripts/Jobstatus.txt
#Printing Output in Jobstatus.txt
Jobstatus=/QIS2FTP/scripts/Scripts/Jobstatus.txt
echo "JobName,StartDateTime,EndDateTime,Status " >>$Jobstatus
echo "------------------------------------------------------------" >>$Jobstatus
#set -x
# import my environment
. /QIS2FTP/dsclientprod/home/my.env
#Variables declaration
#List of all jobs in DS QIS2 Project
DSJoblist=`dssearch -ljobs -matches -sub -oc QIS2 * |cut -f2 -d'\'|awk '{print $1}'`
Project=QIS2
SysDate=`TZ=EDT+8:00 date +%Y%m%d%H`
for i in $DSJoblist
do
   #DSJobStatus variable for collecting Job start and End time,Job duration and Job status
   DSJobStatus=`dsjob -report $Project $i 2> /dev/null | grep "Job" | cut -f2 -d'='`
   StartTime=`echo $DSJobStatus | cut -f1,2 -d' '`
   EndTime=`echo $DSJobStatus | cut -f3,4 -d' '`
   JobStat=`dsjob -report $Project $i | grep status | awk -F= ' { print $2; } ' | awk ' { print $1; } '`;
   JobName=`dssearch -ljobs -matches -sub -oc QIS2 $i`
   #Dateformat 20110805
   JobRunDate=`echo $StartTime |awk '{print $1}'|sed 's/-//'|sed 's/-//'`
   #Time in hours ex: 09
   JobTimeH=`echo $StartTime |awk '{print $2}'|cut -f1 -d':'`
   JobStTime=`echo "$JobRunDate$JobTimeH"`
   #Jobs Running 4 hours before to systemtime
     if [ "$JobStTime" -ge "$SysDate" ] 
        then
                if [ "x1" == "x${JobStat}" ] || [ "x0" < "x${JobStat}" ] || [ "x2" != "x${JobStat}" ]
                    then
                          echo "$JobName,$StartTime,$EndTime,$JobStat"  >>$Jobstatus
                 fi
     fi
 
done
#set +

Erlier it was not going back to previous day when system time is 00:00 (12 AM).
If time is 00,01,02,03 out put should come previous day executed jobs list like wise it should go back previous month and year if the date month and year end.

Thanks Guys i solved it ...