Cron Logging Issue - cron

I cannot find documentation on how to set up Cron commands for some reason. Here's my current Cron command using GoDaddy's cron job manager tool. It runs twice an hour.
/web/cgi-bin/php5 "$HOME/~path-to-file~/my-function.php" > "$HOME/~path-to-file~/my-function.log"
What I'd like to do is get a thorough log of each time the Cron runs - output, timestamp, etc. I'm trying to debug this script and I'm stuck without some kind of error logging.
Thanks all!

You are directing Standard Output to the specified file by >, but Standard Error gets sent into oblivion. Add 2>&1 to the end of the command to append any errors to the same file.
/web/cgi-bin/php5 /path/to/php > /path/to/logfile.log 2>&1

Related

Show the results of jobs scheduled with cron in the terminal

I've sucessfully configured a script that scans my system using cron in a scheduled hour of a day. However, I would like to see the results that it might have generated with its execution. Ex: rkhunter -c leaves logs in /var/log/rkhunter.log that I manually would have to check after it performed the task.
How can I configure cron to show a terminal with its results for example?
I've seen a similar case to mine, but it didn't work:
minute hour * * * DISPLAY=:0 xterm -e /dir_to_my_script/script.sh
But it gave the following error that I've not been able to solve so far:
Warning: This program is an suid-root program or is being run by the root user.
The full text of the error or warning message cannot be safely formatted
in this environment. You may get a more descriptive message by running the
program as a non-root user or by removing the suid bit on the executable.
xterm: Xt error: Can't open display: %s
Is there a way to accomplish it using cron as root? Thanks

Cron job mysteriously stopped running?

I have a cron job on an Ubuntu 10.4 server that stopped running for no apparent reason. (The job ran for months and has not been changed.) I am not a *nix guru so I plead ignorance if this is a simple problem. I can't find any reason or indication why this job would have stopped. I've restarted the server without success. Here's the job:
# m h dom mon dow command
0 * * * * java -jar /home/mydir/myjar.jar >>/home/mydir/crontaboutput.txt
The last line in the output file shows that the program ran on 8/29/2012. Nothing after that.
Any ideas where to look?
There should be something in your system log when the job was run. The other thing you could >try is to add 2>&1 to the job to see any errors in your text file. – Lars Kotthoff yesterday
This proved to be the key piece of information - adding 2>&1 allowed me to capture an error that wasn't getting reported anywhere else. The completed command line then looked like:
java -jar /home/mydir/myjar.jar 2>&1 >>/home/mydir/crontaboutput.txt
Perhaps your cron daemon has stopped, or changed configuration (i.e. /etc/cron.deny). I suggest to make a shell script, and running it from crontab. I also suggest to run thru your crontab some other program (just for testing) at some other time. You can use the logger command in your shell script for syslog. Look into system log files.
Accepted answer is correct, (i.e, check the error logs) which pointed out the error in my case. Besides check for the following issues
include('../my_dir/my_file.php) may work from url but it will not work when cron job is run, will spit out error.
$_SERVER variables does not work inside cron os if you are using $_SERVER['DOCUMENT_ROOT'], it will not be recognized and you will have an error in the cron job.
Make sure to test the cron and have it run, send an email etc to make sure it is run.

Crontab for script

My script is under /u01/software/aditya/script/ directory. Name of script is myscript.sh. I am able to run this script and getting output too. I am trying to set a cronjob for this script at 6.30 daily morning. I am doing this as root user. I have done following steps but not getting output.
crontab -e
30 06 * * * sh /u01/software/aditya/script/myscript.sh >> /u01/software/aditya/hello.log
:wq
but not getting any update in hello.log file :( . please help….
First check your cron log file which is usually in /var/log/syslog. There should be entries similar to
Sep 17 06:30:01 localhost CRON[17725]: (root) CMD (sh /u01/software/aditya/script/myscript.sh >> /u01/software/aditya/hello.log)
If not, your script has never been run. This could be due to a broken crontab file. You should make sure that this file always ends with a newline, better insert more than one at the end so that deleting one accidentally won't break the file.
If this line exists in the log file then your script has been run, but didn't generate any output. This can happen due to a different environment when being run via cron.
Also note that >> only redirects stdout, not stderr. If you want to redirect stderr too, then add 2>&1 at the end of the line.
Normally this is caused by a PATH problem. There is a very good chance that myscript.sh calls a command that is not available in the PATH that cron runs with. Some options to fix this are:
Make sure that every command in myscript.sh is a full path-reference (tedious)
Add source ~/.bashrc to the top of myscript.sh
Add export PATH=$PATH:<colon delimited list of paths necessary for myscript.sh to run correctly>
Pick one of the above, or you could also choose one of the options here: Hourly cron job did not run

How to test the script deployed with crontab

I have a script which I need to run daily at 01.00 am of every month. This is what I did with cron tab to make it happen
#housekeeping
0 1 1 * * sh /product/abc/tools/housekeeper.sh -t ABC
How can I check whether this script is running? Do I have to wait till it runs or is there a process running for this script so that I can confirm it is running?
The usual approach to test crontab scripts follows these guidelines:
Set the time pattern to "ten minutes from now". Wait ten minutes. Check cron's logfile and the log files of your script/app for errors.
Set the desired time pattern
This way, you can make sure that the script works. If something breaks, it must be because of the time pattern.
You should also redirect all output of the script to a log file. cron will send a mail with any output that the script produces but that's rarely desired.
If you always add an entry like "${timestamp} Started" to the log, you can check at your leisure that the script worked as expected.
If you want both, then you must make your script/app write the log file itself (i.e. without output redirection in the crontab) and print any errors to stdout; this will then make cron send an email.
Simple thing I use is to append something to a particular file before and after the desired command, like
*/2 * * * * (echo "Starting" >> ~/monitorCron.log; <Actual operation>; echo "End" >> ~/monitorCron.log;) > ~/cron.log 2>&1
This runs every 2 minutes, and appends the strings to ~/monitorCron.log.
This is the same thing I do in programming, writing a print statement where-ever and when-ever I get a doubt.

Cron job from cpanel.. no success

I tried a lot of methods but ended up with nothing in hand..My simple target is to reset a variable to zero at the end of the day.
i checked the location of php as "which php" and "whereis php"
which resulted into /usr/bin/php
here are some of things i tried..
/usr/local/bin/php -f /home/USERNAME/public_html/developer3/crons/filename.php
/usr/bin/php -f /home/USERNAME/public_html/developer3/crons/filename.php
php -q /home/USERNAME/public_html/developer3/crons/filaname.php
/usr/bin/wget -O http://subdomain.sitename.com/crons/filename.php
for quick results, i kept the timming as every minute to execute the code. i could successfully execute the code as
http://subdomain.sitename.com/crons/filename.php
please guide me.
If the path to php on your system is /usr/bin/php then the command you should be running with cron should be
/usr/bin/php /full/path/to/php/script.php
Is the script you're running designed to be invoked from the php-cli in that way? If it isn't and works correctly when you use a browser then you can use curl, if it's installed on your server
curl -A cron-job http://subdomain.sitename.com/crons/filename.php
It would likely be helpful in any event to configure the MAILTO variable (since you're using cPanel, it's just a text box on the appropriate page that you can fill in) so that you get an email with the output from your cron jobs. Having the output emailed to you will help you diagnose what's causing your script to not have the desired effect when it runs.

Resources