LinuxQuestions.org
Register a domain and help support LQ
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 12-03-2007, 02:43 AM   #1
yusufs
Member
 
Registered: Oct 2007
Posts: 162

Rep: Reputation: 30
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
 
Old 12-03-2007, 04:31 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.
 
Old 12-03-2007, 05:45 AM   #3
yusufs
Member
 
Registered: Oct 2007
Posts: 162

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by matthewg42 View Post
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.



Thanks mathew..I removed the environmental varible and I gave the path..but even then it is not executing..

Comment pls

Yusuf
 
Old 12-03-2007, 07:26 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by yusufs View Post
Thanks mathew..I removed the environmental varible and I gave the path..but even then it is not executing..

Comment pls

Yusuf
What do you mean?
 
Old 12-03-2007, 07:47 AM   #5
yusufs
Member
 
Registered: Oct 2007
Posts: 162

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by matthewg42 View Post
What do you mean?
No environmental variables are stored in the script..


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
 
Old 12-03-2007, 08:33 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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:
  • ORA-
  • ORA--
  • ORA---
  • ORA----
* in the context of a regular expression (which is what grep expects as it's first argument) means "the previous expression atom repeated 0 or more times". The previous atom in this case being the hyphen character.

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
or
Code:
source your_script_filename
...then that is the reason. Scripts which are invoked using the . or source commands are executed in the current shell. If you try to invoke these files as programs (i.e. by specifying there path and expecting the OS to treat them as an executable), you need to do two things:
  • Add a shabang line at the top of the script. This must be the first line of the script file, and there must be no space before the # character:
    Code:
    #!/bin/sh
  • You must also make the program executable, by setting the mode (permissions) of the file. You do this with the chmod command, probably like this:
    Code:
    chmod 755 your_script_filename

So here is how I would change the script:
Code:
#!/bin/sh

# 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 erp_webmail@ali.com.kw
    # what is this for?  $LOGFILE
    exit 1
fi
There is nothing in this script to generate the log which is mentioned, but at least the notification should be sent.

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.

Last edited by matthewg42; 12-03-2007 at 08:33 AM. Reason: typo
 
Old 12-03-2007, 09:21 AM   #7
yusufs
Member
 
Registered: Oct 2007
Posts: 162

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by matthewg42 View Post
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:
  • ORA-
  • ORA--
  • ORA---
  • ORA----
* in the context of a regular expression (which is what grep expects as it's first argument) means "the previous expression atom repeated 0 or more times". The previous atom in this case being the hyphen character.

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
or
Code:
source your_script_filename
...then that is the reason. Scripts which are invoked using the . or source commands are executed in the current shell. If you try to invoke these files as programs (i.e. by specifying there path and expecting the OS to treat them as an executable), you need to do two things:
  • Add a shabang line at the top of the script. This must be the first line of the script file, and there must be no space before the # character:
    Code:
    #!/bin/sh
  • You must also make the program executable, by setting the mode (permissions) of the file. You do this with the chmod command, probably like this:
    Code:
    chmod 755 your_script_filename

So here is how I would change the script:
Code:
#!/bin/sh

# 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 erp_webmail@ali.com.kw
    # what is this for?  $LOGFILE
    exit 1
fi
There is nothing in this script to generate the log which is mentioned, but at least the notification should be sent.

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.



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
 
Old 12-03-2007, 09:37 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by yusufs View Post
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
Do not run it with "sh script.sh". This runs the program in the current shell, and this it not what cron does. Cron tried to execute it as an executable, and for that you must chmod it and add the shabang as I described above.

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
head -n 1 script.sh
mount |grep " $(df . |tail -n 1 |awk '{ print $NF }') "
ls -l /bin/sh
 
Old 12-04-2007, 01:42 AM   #9
yusufs
Member
 
Registered: Oct 2007
Posts: 162

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by matthewg42 View Post
Do not run it with "sh script.sh". This runs the program in the current shell, and this it not what cron does. Cron tried to execute it as an executable, and for that you must chmod it and add the shabang as I described above.

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
head -n 1 script.sh
mount |grep " $(df . |tail -n 1 |awk '{ print $NF }') "
ls -l /bin/sh
Thanks mathew.. here what u asked

[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
 
Old 12-04-2007, 02:05 AM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by yusufs View Post
Thanks mathew.. here what u asked

[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
You didn't use [code] tags, but nevermind.

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
(from the directory where the file is)...?
 
Old 12-04-2007, 02:19 AM   #11
yusufs
Member
 
Registered: Oct 2007
Posts: 162

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by matthewg42 View Post
You didn't use [code] tags, but nevermind.

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
(from the directory where the file is)...?
Thanks mathew,

if I give the command like ./alert.sh
it executes and sends the mail..

Guide please

Yusuf
 
Old 12-04-2007, 02:45 AM   #12
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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
echo -e '#!/bin/bash\ndate > /tmp/my_alert_test.log\n' > alert.sh
chmod 755 alert.sh
Then wait for the cron job to execute and check that the file /tmp/my_alert_test.log has been created and has the expected date and time in it.

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.
 
Old 12-04-2007, 03:13 AM   #13
yusufs
Member
 
Registered: Oct 2007
Posts: 162

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by matthewg42 View Post
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
echo -e '#!/bin/bash\ndate > /tmp/my_alert_test.log\n' > alert.sh
chmod 755 alert.sh
Then wait for the cron job to execute and check that the file /tmp/my_alert_test.log has been created and has the expected date and time in it.

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.




[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
 
Old 12-04-2007, 03:33 AM   #14
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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?
 
Old 12-04-2007, 05:09 AM   #15
yusufs
Member
 
Registered: Oct 2007
Posts: 162

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by matthewg42 View Post
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?
Mathew,

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
 
  


Reply

Tags
chmod, cron, grep, script


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
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 09:16 AM
cron not working from crontab nor form /etc/cron/cron.d. What did SuSE change? JZL240I-U Suse/Novell 11 01-04-2007 02:57 AM
Cron not Executing PHP script Badnoodles Linux - General 2 10-25-2006 05:56 PM
Can any one plz explain why/what for cron.d, cron.daily, cron.weekly etc are there. mavinashbabu Linux - Newbie 4 09-21-2006 02:50 PM
Executing cron jobs on a w2k3 server using cygwin hq4ever General 4 06-01-2005 07:45 AM


All times are GMT -5. The time now is 01:44 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration