Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unix script help required

Status
Not open for further replies.

toronto2003

IS-IT--Management
May 5, 2003
14
CA
Hello,

I need help writing a unix script and then have it as a cronjob running everyday at 6:00 a.m

There are three .csv files that get generated every day via other job on \disk2\jobs directory:

The files under this directory end with the current date:
A_YYYYMMDD.csv
B_YYYYMMDD.csv
C_YYYYMMDD.csv

I need to do a record count on all these three .csv files and send an email along with the attachments to an email alias. I also need to send an alert if either of this three files are not found in the /disk2/jobs directory at 6:00 a.m every day.

Your help on this would be highly appreciated.

Thanks a lot.
 
OK, I'll do part of it .....

#!/bin/ksh

d=$(date "+%Y%m%d")
af="A_$d.csv"
bf="B_$d.csv"
cf="C_$d.csv"
my_dir=/disk2/jobs

for i in $af $bf $cf
do
if [ ! -f $my_dir/$i ]; then
echo "file $i not there"
fi
done

 
Building on olded's code...
Code:
#!/bin/ksh

export d=$(date "+%Y%m%d")
export af="A_$d.csv"
export bf="B_$d.csv"
export cf="C_$d.csv"
export my_dir=/disk2/jobs
export maildest=person@mail.com  # change to valid email

(
print "Daily csv files as of $(date)"
print
for i in $af $bf $cf
do
   if [ ! -f $my_dir/$i ]
   then
      print "File $i not found!"
   else
      print "File $i present with $(cat $i|wc -l) records."
   fi
done
print "See attachments"
print
for i in $af $bf $cf
do
   uuencode $i $i
done
) | mailx -s "Daily csv files as of $(date)" ${maildest}
Hope this helps.

 
Hello Olded & Sambones

thanks for your help on this. I really appreciate it.

Can i ask for one more favor. What should i do in order to test this script before I schedule it to run as a cronjob at 6:00 am every morning.

Would i be able to test the script for today's files that got generated and see if the email gets send out with the record count on each file.

Also how do i schedule it as a cronjob later on to run everyday at 6:00 a.m morning.

Your help on this would be highly appreciated.

Thanks.
 
Hi:

Of course, test it from the command line to make sure it works the way you want.

Do a MAN crontab, but here's at sample entry for each morning at 0600:

0 6 * * * cd /whereverSamBonesscript_is; SamBonesscript > optional_output_file

Make sure you define PATH in your script. That's the most common problem when something runs from the command-line, but not from cron.

Regards,


Ed


 
Hello Olded,

Thanks for your help on this. I did manage it out after i posted this question.

One quick question though, if you would be able to help with :- )

the files that i'm looking for and doing a count get generated via other job at some time after 12:10 a.m

How can i have the script start running after say 12:01 a.m and keep running until it finds the files that get generated on /disk2/jobs directory. I want it that as soon as it finds the files do the job, wait maximum till 4:00 a.m and if it doesn't find then report that the file is not found.

Thanks
 
Hello,

One other question please, how can i have the files with the path rather than having them as attachment?

Thanks
 
Hi:

You would have to use a combination of cron and script change:

This would run the job at 15, 30, and 45 past the hour from midnight to 0400:

# untested
15,30,45 0-4 * * * scriptstuff

You'd have to do the check within the the script for the specific time:


# untested

tim=$(date "+%H%S") # get 24 hour time
if [ ! -f $my_dir/$i ]
then
if [[ $tim < &quot;0400&quot; ]]
then
continue # dont report file missing till 0400
else
print &quot;File $i not found!&quot;
.
.

I don't understand your other question.

Ed
 
Hello Olded,

thanks for your reply back and help on this.

The other question i meant was based on SamBones script it sends the email out with the attachments of the files including the record count in the files.

I want to have the email send out NOT with the attachments but with the file path, example

\disk2\jobs\A_YYYYMMDD.csv present with xx records
\disk2\jobs\B_YYYYMMDD.csv present with yy records
\disk2\jobs\C_YYYYMMDD.csv present with zz records

Thanks
 
Hello,

As per the above script how can i two different messages, one if the job is completed and other if the job is failed?

thanks,
 
Hi:

Just remove the uuencode loop, and prepend the $my_dir/ to the front of $i:

# untested
print &quot;Daily csv files as of $(date)&quot;
print
for i in $af $bf $cf
do
if [ ! -f $my_dir/$i ]
then
print &quot;File $my_dir/$i not found!&quot;
else
print &quot;File $my_dir/$i present with $(cat $i|wc -l) records.&quot;
fi
done
print
) | mailx -s &quot;Daily csv files as of $(date)&quot;
${maildest}
 
Hello Olded,

I appreciate for all of your help here. this part worked.

Earlier when you helped me with the job timing part i still need some help on it.

I tried to scheudle the crontab on 15, 30, and 45 past the hour from midnight to 0400 and it keeps on running at all the times till 0400 whether it finds or doesn't find the files till 0400.

How can i say that start the job at midnight, keep it running till 0400 but during this interval once it finds all the three files exit out with an email message saying &quot;COMPLETED AS OF:{DATE}&quot;, by 0400 max if it doesn't find EITHER one or all of the three files exit out with an email message saying &quot;FAILED AS OF: {DATE}&quot;.

Sorry for bugging you since last 2/3 days, but i'm desperate to get this done and would appreciate if you could help me out with it.
Thanks,
Alpesh
 
Hi:

Do you want something like this:

typeset -i tim
tim=$(date &quot;+%H%S&quot;) # get 24 hour time
if [[ $tim -gt 400 ]]; then
if [[ -f $my_dir/$af && -f $my_dir/$bf && -f $my_dir/$cf ]]; then
echo &quot;completed as of $d&quot;
else
echo &quot;failed as of $d&quot;
fi
fi
# end script, untested

In one of my previous emails, I may have mislead you on the string comparision code. In this version, I typeset a variable to an integer in order to do a comparsion. Now, if all three files exist, it reports printed, else failed. you'll have to wedge this somewhere into the other code.

Ed
 
Hello Olded,

This is my version based on your help but doesn't seem to work. I know there are some mistakes I've made but not able to figure it out. Can you please let me know what needs to be corrected.

I'm confused with the mail part, the subject and the body of the email for both the Completed and Failed.

#!/bin/ksh

export d=`date &quot;+%Y%m%d&quot;`
export ls=&quot;AA_${d}_IAS.csv&quot;
export lr=&quot;AB_${d}_IAS.csv&quot;
export ms=&quot;BA_${d}_IAS.csv&quot;
export mr=&quot;BB_${d}_IAS.csv&quot;
export rs=&quot;CA_${d}_IAS.csv&quot;
export rr=&quot;CB_${d}_IAS.csv&quot;
typeset -i tim
tim=$(date &quot;+%H%S&quot;) # get 24 hour time

export my_dir=&quot;/disk2/jobs/import&quot;

cd ${my_dir}

export maildest=&quot;test@abc.com&quot; # change to valid email

(
print &quot;Daily csv files as of $(date)&quot;
print
#for i in $ls $lr $ms $mr $rs $rr
if [[ $tim -gt 0400 ]] then
if [[ -f $my_dir/$ls && -f $my_dir/$lr &&
-f $my_dir/$ms && -f #my_dir/&mr &&
-f #my_dir/&rs && -f $my_dir/$rr && ]]
then
echo &quot;completed as of $d&quot;
else
echo &quot;failed as of $d&quot;
fi
fi
print
) | mailx -s &quot;Daily csv files as of $(date)&quot; ${maildest}


Thanks,
Alpesh
 
don't you mean this?

if [[ -f $my_dir/$ls && -f $my_dir/$lr &&
-f $my_dir/$ms && -f $my_dir/$mr &&
-f $my_dir/$rs && -f $my_dir/$rr && ]]

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
ooops:

if [[ -f $my_dir/$ls && -f $my_dir/$lr && -f $my_dir/$ms && -f $my_dir/$mr && -f $my_dir/$rs && -f $my_dir/$rr ]]



vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Yes, i noticed that type,fixed it and it complains

./P2I[17]: syntax error at line 27 : `newline or ;' unexpected
 
try my last post

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi,

finally it worked now... How can i different email subject for failed and completed jobs?

Thanks,
 
Hi:

set up a different subject line:

if ....
then
subjectline=&quot;completed as of $d&quot;
echo $subjectline
else
subjectline=&quot;failed as of $d&quot;
echo $subjectline
fi

Now, change the mailx command to something like this:

mailx -s &quot;$subjectline&quot; ${maildest}


Regards,

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top