LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-01-2011, 09:33 PM   #1
mad_penguin
Member
 
Registered: Mar 2008
Posts: 69

Rep: Reputation: 15
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.
 
Old 09-01-2011, 09:42 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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.
 
Old 09-01-2011, 09:47 PM   #3
mad_penguin
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
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.
 
Old 09-01-2011, 09:59 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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
 
Old 09-01-2011, 10:05 PM   #5
mad_penguin
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
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.
 
Old 09-01-2011, 10:36 PM   #6
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
 
Old 09-01-2011, 10:51 PM   #7
mad_penguin
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
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..
 
Old 09-01-2011, 10:54 PM   #8
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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
 
Old 09-02-2011, 12:12 AM   #9
mad_penguin
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
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.
 
Old 09-02-2011, 12:24 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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

Last edited by chrism01; 09-02-2011 at 12:26 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linux cron job duplicate job question cpthk Linux - Newbie 4 09-11-2009 09:52 PM
Cron Job Help grande25 Programming 35 11-02-2008 01:42 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 09:16 AM
cron job somsahi Linux - Software 9 10-05-2006 03:19 PM
Cron job rust8y Linux - Newbie 3 05-18-2006 10:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration