LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 03-02-2006, 05:48 PM   #1
kazador
LQ Newbie
 
Registered: Mar 2006
Distribution: RHEL4
Posts: 8

Rep: Reputation: 0
cron output problems


Thank you in advance for your help.
I wrote a simple shell script to append text to a file. At the end of the file i used the tail command to view the the last lines of appended text form the file. When I execute the file from the command line with a ./ it seems to work fine. However, when i set the command up to run as a cronjob (crontab -e) the job runs,
but the only output I get is to mail(and to the file). How do I get the output to display to stdout?

Thanks again
Kazador
 
Old 03-02-2006, 06:38 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,125

Rep: Reputation: 164Reputation: 164
By default if cron generates output on stdout or stderr the result will be mailed to the user owning the cron job. This is because cron jobs are often run when nobody is logged in and there is nowhere for output to be displayed.

You could try something like the following in your cron job:
Code:
(/usr/local/bin/script > /tmp/script.log && tail -n 20 /tmp/scipt.log) | mail -s "Script finished" username
I haven't tested this, but the basic structure is something I use here.
 
Old 03-02-2006, 10:56 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,294

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
Basically, running a prog like this

./prog

runs the prog attached to console and stdout/stderr defaults to there

./prog &

puts prog in 'background', but stdout/sterr defaults to console

nohup ./prog &

detaches prog from console and puts prog in background. stdout/stderr defaults to nohup.out in orig dir.

cron is a daemon server that runs everything in 'nohup ./prog &' style, but stout/stderr defaults to email.

HTH
 
Old 03-03-2006, 02:28 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
gilead, Vixie cron (ie cron written by Paul Vixie), and probably also some other flavors of cron, support adding
MAILTO=some_mail_address
at the top of the crontab file to send the stdout/stderr of cron jobs to the specified mail address, rather than to the owner of the cron job (default).

MAILTO="" makes sure that the output doesn't get mailed at all (stdout/stderr are then simply disregarded).

kazador, it's rather hard (if not impossible) to make cron's stdout/stderr appear let's say on your terminal window (ie your shell's stdout). Imagine what would happen if the cron job runs while you aren't logged in or don't have an open terminal window. Furthermore, how can cron know to which terminal window it's supposed to send the stdout/stderr to? Cron itself runs independently from anything you're running. It just runs the jobs you defined for it, as if they were run by you (so, under your user) and sends the stdout/stderr via e-mail to you.
Alternatively, you could catch the output of your "tail" command in a file and then do a "cat" of that file in your terminal window to see what "tail" was outputting. If you don't like the e-mails.
 
Old 03-03-2006, 10:52 AM   #5
kazador
LQ Newbie
 
Registered: Mar 2006
Distribution: RHEL4
Posts: 8

Original Poster
Rep: Reputation: 0
Thank you all for your input. Your comments and knowledge have been very helpful.I will try your suggestions.

Regards
Kazador
 
Old 03-03-2006, 02:32 PM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,125

Rep: Reputation: 164Reputation: 164
Hi timmeke, thanks for that. I didn't know what distribution that kazador was using, so I left my response as general as possible. For example, Dillon's cron does not support MAILTO in crontabs. At least, not in the man pages for crontab or crond, and the variations that I tried all continue to email to the owner of the cron job.

Piping the output to mail works regardless of the distro used. As an alternative, the cron docs suggest that if the job is being run as a special user (e.g. UUCP) that a mail alias be set up so that the mail is diverted to a regular user. Aliases didn't sound applicable to the original question so I didn't bother mentioning them previously.
 
  


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
cron output nelamvr6 Linux - Newbie 3 10-18-2005 07:11 PM
Can the output of cron daemon be sent via other smtp hamish Linux - Software 0 09-02-2005 08:28 AM
Cron output to a file Temujin_12 Linux - General 5 02-23-2005 01:52 PM
cron output? sk8guitar Linux - General 4 08-15-2003 01:55 PM
Understanding CRON output lhoff Linux - General 1 05-19-2003 01:17 PM


All times are GMT -5. The time now is 12:52 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