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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

redirecting cron output into script then into file.

Status
Not open for further replies.

vortmax

Technical User
Aug 1, 2006
46
US
I'm using cron and fetchmail to poll a mail server on a regular basis. For sake of redundant record keeping, I need to log the output of the fetchmail command every time it is run. This is not hard, as all I need to do is redirect it to a log file with a >> in the cron line.

My issue is that this method produces the file I want, but there are no time or date stamps. I wrote a small script in perl that does nothing but take a line as input, append a time stamp to the front, then print the combined line. So in theory, all I need to do is pass the output of fetchmail into this perl script then redirect its output into a log file.

So now I have 2 questions:

1. When I pass the output of fetchmail (on the command line) into this script (addts) with:

Code:
fetchmail >(addts)

I get this error:
Code:
helpdesk@helpdesk:~/tmp$ fetchmail >(addts)
2008-04-25 14:20:301 :Enter password for helpdesk@/dev/fd/63:

entering the password I get:
Code:
fetchmail: couldn't find canonical DNS name of /dev/fd/63 (/dev/fd/63): Name or service not known
fetchmail: Query status=11 (DNS)

I assume this has something to do with the shell fetchmail is being run in? Just calling fetchmail works fine.

2. I can redirect the output of fetchmail into the addts script, but how can I then redirect that output into a log file all on one line? Would it be something like:

Code:
fetchmail >(addts) > logfile
 
Wouldn't that be...
Code:
fetchmail | addts >> logfile
You can also do it with just a function...
Code:
#!/bin/ksh

function time_stamper {
   while read LINE
   do
      print "$(/bin/date '+%Y%m%d:%H:%M:%S'): $LINE"
   done
}

fetchmail | time_stamper >> logfile


 
thanks,
that worked. I originally tried using the pipe, but it would break with the perl script. Switched it over the bash script and it's working well. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top