LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2008, 12:18 AM   #1
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Rep: Reputation: 15
Cron output to .log


Hi,

I have a command that i want to execute and put the results to a file in /var/log called myfile.log. I've put the command into a shell script and when i run the script manually, i get the output on the command line.

When i add a cron job to execute the script and put the same result into the log file, nothing is put in the log file.

Here is my cron job:

*/01 * * * * '/home/nagios/scr/check_oracle_generic.sh > /var/log/app_cron.log'

Here is what /var/log/cron shows:

crond[11014]: (userid) CMD ('/home/anyuser/scr/check_oracle_generic.sh > /var/log/app_cron.log')

so cron runs the command but nothing goes into the log file.

Here are the contents of my script:

#!/bin/sh
/usr/lib64/nagios/plugins/check_oracle_generic -SID=server fqdn
-dbuser=userid -dbpassword=useruser password -w=585 -c=600 -q="select count(*) from c_asset_version where markup_status='PENDING'"

printf "\n" >> /var/log/app_cron.log


This is what should be outputed from this script:

OK=565


Thanks in advance for anyones help
 
Old 04-25-2008, 01:52 AM   #2
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
If within the shell script, the putput file is appended to (>>), then why do you have to have the redirect (>) in the cron command?

It looks like the reason - particularly if the shell script works directly. Another thing could be permissions.

end
 
Old 04-25-2008, 01:58 AM   #3
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,629

Rep: Reputation: Disabled
Did you check the permissions on /var/log? Do they match with those of the script?
 
Old 04-25-2008, 09:01 AM   #4
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
I've changes the >> in the script to match what is in the cron and still no success. The permissions on the script and the file its writing to are identical.

Thanks for those tips. If anyone has any other, please let me know.
 
Old 04-25-2008, 09:17 AM   #5
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,629

Rep: Reputation: Disabled
When you run that script by hand, do you get the desired output on stdout or to the file you specified?

Because what you do is writing doubly: once by "printf" and again by the ">" after your script is run by cron.

Please always repost your script (and crontab) after changes, so we can see exactly what you did.
 
Old 04-25-2008, 09:45 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
In a cronjob you don't have access to stdio or stderr. The process may be blocked from running if it prints to stderr. You redirected stdio, but some commands output the information using stderr. Either redirect stderr to stdout or to null.
 
Old 04-25-2008, 10:40 AM   #7
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
Smile

Thanks for that! So how do i do that? Sorry my scripting isnt that good.
 
Old 04-25-2008, 12:20 PM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by investmentbnker75 View Post
Thanks for that! So how do i do that? Sorry my scripting isnt that good.
If you put the CRON job in with a 2>&1 at the end, it should burp out the stdio and stderr into the log file.
 
Old 04-25-2008, 02:01 PM   #9
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
Thanks tbone and everyone who is trying to help out. I really appreciate it since it is very important for me to get this to work. But tbones suggestion failed also. It just input a blank space into the log file.

Heres why i think its failing. When i run the command or the script manually, the output is return too close to the next prompt and cant interpret any output when run by cron. See example below:

OK: result = 565root@servername:scr#

If there is a way to get the cron to run and return the output on a line above the next prompt? I've edited my script to include sleep. Here is my script below:

#!/bin/sh
/usr/lib64/nagios/plugins/check_oracle_generic -SID=server name -dbuser=dbuser -dbpassword=dbpasswd -w=585 -c=600 -q="select count(*) from c_asset_version where markup_status='PENDING'"
sleep 5
printf "\n" > /var/log/app_cron.log


My cron entry:

*/01 * * * * /home/nagios/scr/check_oracle_generic.sh > /var/log/app_cron.log 2>&1
 
Old 04-25-2008, 07:55 PM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You still are using ">" instead of ">>" in your cron script. You want to append to a log file and not overwrite it. Change it in both the cron entry and the script. You don't need the sleep command.

I assummed that your script is generalized for this post because the arguments like "-SID=server name" are wrong.

Also consider using the "logger" command to create log entries inside the script, based on the return value of the command.

Last edited by jschiwal; 04-25-2008 at 08:14 PM.
 
Old 04-26-2008, 10:57 PM   #11
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
No i do want the file over written everytime, thats why im using the > and not the >>. As far as the -SID, it needs to be there for the script to work and it runs when i run the command manually command line and when i put it in the script and run it. I just need to know how to get the output to actually dump to a file as it shows command line.
 
Old 04-27-2008, 12:44 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
if this is working:

Code:
*/01 * * * * '/home/nagios/scr/check_oracle_generic.sh > /var/log/app_cron.log'
your stdout should already be redirected to the file

and just doing printf "\n" inside the script should already redirected the message to the output file so you don't need to redirect it again

Code:
#!/bin/sh

/usr/lib64/nagios/plugins/check_oracle_generic -SID=server fqdn
-dbuser=userid -dbpassword=useruser password -w=585 -c=600 -q="select count(*) from c_asset_version where markup_status='PENDING'"

printf "\n"
i don't know if your scripts are already working but there are two ways i know on how to do this

(a) first is redirect it in the crontab command:
Code:
*/01 * * * * '/home/nagios/scr/check_oracle_generic.sh > /var/log/app_cron.log 2>&1'
(b) or redirect it inside the script:
Code:
*/01 * * * * '/home/nagios/scr/check_oracle_generic.sh'
Code:
#!/bin/sh

exec > /var/log/app_cron.log 2>&1

/usr/lib64/nagios/plugins/check_oracle_generic -SID=server fqdn
-dbuser=userid -dbpassword=useruser password -w=585 -c=600 -q="select count(*) from c_asset_version where markup_status='PENDING'"

printf "\n"

Last edited by konsolebox; 04-27-2008 at 12:45 AM.
 
Old 04-27-2008, 06:13 PM   #13
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
Thanks Konsolebox, i appreciate the input but neither one of these worked. In youre first scenario, the cron ran but nothing wrote to the log file.

But when run command line, the correct output was seen.

In the second scenario, it worked command line but in the log file, it had the following error:

output must be to a tty

or it outputs a single blank space

I then added su - nagios before the exec line and command line it works but still the output desired doesnt rach the log file. UGGG now what?
 
Old 04-28-2008, 01:15 AM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
were you already able to log the output to the logfile by just doing something like this in the command-line?

Code:
/usr/lib64/nagios/plugins/check_oracle_generic -SID=server fqdn
-dbuser=userid -dbpassword=useruser password -w=585 -c=600 -q="select count(*) from c_asset_version where markup_status='PENDING'" >/var/log/app_cron.log 2>&1
 
Old 04-28-2008, 07:09 AM   #15
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
Yes, running that sends it there. Also when i put that command into a script it works as well. Cron executing the script wont put the result in the log file for some reason.

It only puts in a single blank space or nothing at all or the tty error i listed.
 
  


Reply



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 yusufs Linux - Newbie 3 11-30-2007 05:28 AM
cron e-mail output fyr3 Slackware 1 07-16-2007 02:51 PM
cron output nelamvr6 Linux - Newbie 3 10-18-2005 07:11 PM
cron output? sk8guitar Linux - General 4 08-15-2003 01:55 PM
dual entries in cron log for cron.daily cpharvey Linux - General 3 02-27-2003 02:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:39 PM.

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
Open Source Consulting | Domain Registration