cron not executing
Dear all,
I have a script to check for errors and send a mail .when the script is executed manually, it works fine.. but when am addin it to the crontab it is not sending any mails.. crontab contents ..I want to run the script every morning at 10:22 am.. followin is the crontab content 22 10 * * * /home/oracrp/test/alert.sh please guide Yusuf |
When jobs run from cron, they do not have the same environment variables as you have with your interactive shell. For example, the PATH will be very limited. Make sure such a limited environment doe snot affect your program.
|
Quote:
Thanks mathew..I removed the environmental varible and I gave the path..but even then it is not executing.. Comment pls Yusuf |
Quote:
|
Quote:
Thanks Yusuf Script content : ***************** #Script used to monitor the database alert log file #LOGFILE=/tmp/database_alert_$ORACLE_SID.log RETURN_CODE=`grep ORA-* /d02/oracle/oracrp/proddb/9.2.0/admin/DEV_oracle/bdump/alert_DEV.log | wc -l` if [ $RETURN_CODE -eq 0 ] then exit 0 else echo "DEV - There are $RETURN_CODE errors in Alert log" | /usr/sbin/sendmail -v erp_webmail@ali.com.kw $LOGFILE exit 1 fi crontab -e ************ 5 15 * * * /home/oracrp/test/alert.sh >/home/oracrp/script.log 2>&1 |
I think the parameter to grep should be simplified. There is no need for the * character at all, and it is risky.
Firstly, the regular expression ORA-* will match all of the following:
Moreover, having an un-quoted * as the first argument to grep is dangerous. If the working directory of the program contains any files matching the glob pattern ORA-* the shell will replace that argument with the list of matching file names, which could lead to unexpected behaviour (since it is not possible to predict what files will ever exist in this directory). Because of the first point about pattern being meaningless, just omit the *. If you really did want a * in the pattern (e.g. if the pattern were ORA-[0-9]* meaning to match ORA- followed by 0 or more digits), you should quote the pattern. Single quotes - not to be confused with backticks - prevent the shell from interpretting anything, and are the best choice here. The line $LOGFILE is meaningless, especially given than you have commented out the definition of the variable. The use of the -v option to sendmail doesn't make ant sense. It means that the next argument is the envelope ID. You should omit this I think. Lastly, and perhaps most importantly, there is no shabang line for your script. How are you invoking this on the command line (i.e. when you don't run it from cron)? If you are doing something like this: Code:
. your_script_filename Code:
source your_script_filename
So here is how I would change the script: Code:
#!/bin/sh If you want to generate the log you are in trouble. The ORACLE_SID value is taken from the environment - which you do not get when running from cron. You will need to either choose a different log file name, or find some way to determine this value without getting it from the environment. |
Quote:
Thanks mathew for your patience and answer.. that really helped me.. but am afraid still I can run the script normally using sh script.sh which sends the mail and when I put it in crontab it is not working the expected way.. Please guide Yusuf |
Quote:
Assuming the script is called script.sh, please execute these commands from the directory where the file exists and paste the output here (use [code] tags to help readability): Code:
ls -l script.sh |
Quote:
[oracrp@oracle test]$ ls -l alert.sh -rwxrwxrwx 1 oracrp dba 409 Dec 3 16:55 alert.sh [oracrp@oracle test]$ head -n 1 alert.sh #!/bin/sh [oracrp@oracle test]$ mount |grep " $(df . |tail -n 1 |awk '{ print $NF }') " /dev/cciss/c0d0p1 on / type ext3 (rw) [oracrp@oracle test]$ ls -l /bin/sh lrwxrwxrwx 1 root root 4 May 24 2007 /bin/sh -> bash Waiting on u .. Thanks Yusuf |
Quote:
Hmm, well everything looks in order there. Just by was of explanation.. the mount mount command was to see if the drive where the file exists is mounted in a way which prevents running of scripts. What happens when you run the script like this: Code:
./alert.sh |
Quote:
if I give the command like ./alert.sh it executes and sends the mail.. Guide please Yusuf |
Well it's very odd that it doesn't when it executes from cron. The next thing I would do is to verify that cron is executing anything. I would do this by temporarily moving the script, and creating another which will just put a timestamp in a file, which you can check:
Code:
mv alert.sh alert_backup.sh This will verify that your crontab is being executed, and that you have edited it correctly. Incidentally, what is in your crontab? One last thing... The permissions on the alert.sh which you have set are very bad indeed. This file is writable by any user or process on the system. This is extremely bad security, even if you think you are the only user on the machine. Don't do it. If you don't understand permissions properly, you should stop now and go read up on them. This is very important. |
Quote:
[oracrp@oracle ~]$ mv alert.sh alert_backup.sh [oracrp@oracle ~]$ echo -e '#!/bin/bash\ndate > /tmp/my_alert_test.log\n' > alert.sh [oracrp@oracle ~]$ chmod 755 alert.sh after the cron is ran, /tmp/my_alert_test.log is not created crontab -l [oracrp@oracle tmp]$ crontab -l #This scripts runs every 1 hour to list the sessions,processes,tablespace size and Invalid object 58 10 * * * /home/oracrp/test/alert.sh #MAILTO="dba@ali.com.kw" #55 10 * * * echo $HOME Please guide Yusuf |
Well it looks like your cron job is not executing at all. Perhaps the user oracrp is not allowed to run cron jobs, or maybe the cron daemon is not running at all?
|
Quote:
Cron is working fine.. I tried creating a sample file with the cron and it created.. the problem with that it is not sending the mails. where i can see the cronjob logs of the user Guide Yusuf |
All times are GMT -5. The time now is 09:10 AM. |