LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cron job (https://www.linuxquestions.org/questions/linux-newbie-8/cron-job-900634/)

mad_penguin 09-01-2011 08:33 PM

cron job
 
Hi gents,

I need some help with this cron job I have to run.

It looks like this:
*/1 * * * * time /home/user/script.pl | mail -s "blah-blah-execution_`date +\%H:\%M_\%D`" user@mydomain.com >/dev/null

As you can easily see I use 'time' before my perl script as I need to know how long the script will take to execute/finish.
Actually, this is the only info I need to get by email. The job runs only that now I get by email the script output results and not the execution time.

chrism01 09-01-2011 08:42 PM

Calling a cron job every minute is going to hammer your system somewhat, as cron had to crate an entire new process environment for each invocation.
My rule of thumb is that for any more frequent that every 5 mins, create a daemon instead, optionally pausing at the bottom of the loop for as long as reqd.
Also, with this soln, you can grab current time at top of processing loop, then again at the bottom & incorporate the email cmd (with time diff) at the bottom of each loop.

mad_penguin 09-01-2011 08:47 PM

Chris, thanks.

Anyway, that's not the point. The script won't be run every minute it will be run once a day actually. I just wrote that for no reason.

My problem is how the hell should I send output of 'time' on mail (I'm not interested in script output) I just need to know how much time my script will take to complete.


Thanks.

chrism01 09-01-2011 08:59 PM

options spring to mind

1. get the Perl program to output elapsed time at the end

2. redirect " time xxx.pl" to a file & email file
http://www.simplehelp.net/2008/12/01...-command-line/

3. capture output in subshell
loosely speaking
Code:

(time /home/user/script.pl ) | mail -s "blah-blah-execution_`date +\%H:\%M_\%D`" user@mydomain.com

mad_penguin 09-01-2011 09:05 PM

Okay, I've tried the last one, number 3.
If I run it in console it will give me the execution time but on email I will get the results/output from the script.
I don't need them ! :( I just need the script running time, that's all.

anomie 09-01-2011 09:36 PM

Note that time(1) outputs to stderr by default. You can use that to your advantage. In your crontab:

Code:

MAILTO="foo@bar.baz"
30 5 * * * time /home/user/script.pl > /dev/null

That's it. Forget about piping output to mail(1). Your MAILTO assignment will take care of it for you.

mad_penguin 09-01-2011 09:51 PM

Hi Anomie,

It still doesn't work. I get the output/result from the script and not the output from 'time'.
As I told before I don't need the output from script because it's a bg script that runs more the 30 minutes and the output is huge. I only need that bloody output from 'time'. It seems that it's harder then I previous thought.


Ermmm.. :(

anomie 09-01-2011 09:54 PM

That doesn't make sense, unless you're specifically writing Perl output to stderr. You have redirected the script stdout to /dev/null.

Please post the results of:
Code:

# crontab -l

mad_penguin 09-01-2011 11:12 PM

anomie, I've managed to find a working solution after all.

It looks like this:

( time ./test.sh ) > /tmp/out 2>&1 && mail -s "test" user@mydomain.com < /tmp/out

Unfortunately it emails all output, once from the script and second from time command. If I could find a way to run the script and just send by email the execution time will be awesome, but for now I'm happy with this.

chrism01 09-01-2011 11:24 PM

A couple of more options:

1. amend the Perl prog to redirect its stdout+stderr to a file (or /dev/null if you REALLY don't need it; BUT then why is it outputting anything?)
In either case you get .pl to only output (to stdout) the reqd elapsed time

OR

2. put the .pl in the subshell only;
my test looks like this
Code:

# without redir: t.sh just does 'echo $SHELL'
 time (./t.sh)
/bin/ksh

real    0m0.01s
user    0m0.00s
sys    0m0.00s

#throw t.sh output away
time (./t.sh >t.t)

real    0m0.01s
user    0m0.00s
sys    0m0.00s



All times are GMT -5. The time now is 04:07 PM.