ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.