MikeKulls
MikeKulls

Reputation: 1036

Perl, determine where STDIN is coming from

We have a number of cron jobs that collect various stats over a rather large network. Most of these cron jobs don't get monitored very well. I wanted to write a perl script that we could pipe the output of these jobs into, something like this:

5 * * * * collectstats.pl 2>&1 1>/dev/null | scriptwatcher.pl

The idea is that stdout from collectstats.pl is discarded and stderr is piped into scriptwatcher.pl. This second script can then take appropriate action in the case of errors, most likely email me. With a quick test this is working except that in scriptwatcher I need to know the name of the script that is sending it errors. With just that one little piece of information everything I want to do becomes possible.

Also, is it possible to pipe the stdout from collectstats into another script at the same time? ie 2 pipes for the one script?

Cheers

Upvotes: 2

Views: 227

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 754700

Use the line:

5 * * * * collectstats.pl 2>&1 1>/dev/null | scriptwatcher.pl collectstats.pl

That is, tell the script watcher which script it is watching.

Alternatively, as ysth suggests in his answer, have scriptwatcher run the script it is supposed to watch, like nohup and su and so on can run other commands for you. This avoids you having to name the script twice. If repetition is a problem, choose ysth's answer, please.

For your auxilliary question:

is it possible to pipe the stdout from collectstats into another script at the same time?

Look up the pee command, parallel to tee but with processes (hence the p) instead of files. (Be careful: 'pee command' isn't a good Google search term, but the URL I point to is an SO answer with more information about it and direct links to where you can find the code, etc.)

Upvotes: 1

ysth
ysth

Reputation: 98398

Turn it around and do:

5 * * * * scriptwatcher.pl collectstats.pl any extra args

and have your scriptwatcher.pl deal with the redirection and running the script given in its args.

Upvotes: 4

Related Questions