1748069 Members
5291 Online
108758 Solutions
New Discussion юеВ

cron Entry not working

 
s.kalaivanan
Advisor

cron Entry not working

Hi All ,

I'm facing problem in crontab entry execution
1. I have a .sh script in /tmp/bdf/fs.sh
2. this script is running fine by manual ( #sh fs.sh )
3.Edited crontab -e and put entry like
0 11 * * * /tmp/bdf/fs.sh ( everyday 11am )
4. file permission is ok
-rwxrwxrwx 1 root sys 2077 Apr 23 11:54 fs.sh
but this cron job not running

cron log
>>>> > CMD: /tmp/bdf/fs.sh

syslog

>> : Accepted publickey for root from x.x.x.x

in var/mail/root

Message-Id: <201004270530.o3R5UAMx020211@fqdn.com>
Subject: cron





*************************************************
Cron: The previous message is the standard output
and standard error of one of your crontab commands:



pls give me the solution ,

Thanks & Regards,
Kalai.

9 REPLIES 9
Pete Randall
Outstanding Contributor

Re: cron Entry not working

Usually when a script runs manually but not in cron, it is because of cron's sparse environment. You need to make sure you use full paths for all your commands and files and you need to make sure that all your environment variables are defined. Perhaps you should show us the script?


Pete

Pete
Robert-Jan Goossens
Honored Contributor

Re: cron Entry not working

Ho Kalai,

Most likely cause is missing environmental variables like PATH in your script, if you add the PATH of all your commands in the beginning of the script, it will run.

Robert-Jan
Bill Hassell
Honored Contributor

Re: cron Entry not working

>> 1. I have a .sh script in /tmp/bdf/fs.sh

Not a good place to store anything permanent. Any user can erase this file or rename it.

>> 2. this script is running fine by manual ( #sh fs.sh )

...as mentioned, it runs from your login. Type the command env and you will see that there are a lot of variables defined because you logged in. cron does not login.

>> 3.Edited crontab -e and put entry like
0 11 * * * /tmp/bdf/fs.sh ( everyday 11am )

OK

>> 4. file permission is ok
-rwxrwxrwx 1 root sys 2077 Apr 23 11:54 fs.sh
but this cron job not running

File permission is *NOT* OK. -rwxrwxrwx is the worst possible permission to set for any script, especially a script to be run by root. It should be -rwx------ (chmod 700 /tmp/bdf/fs.sh) so that the contents of the script is protected. This setting will not fix your problem but at least it will keep the script from being trashed by another user.

Your script apparently has no status or error checking internally so to see what is wrong, you will need to trace the script from cron. To do this, add the line:

set -x

after your interpreter line in your script (after the first line which says #!/usr/bin/sh). You do have an interpreter line as line 1, correct?


Bill Hassell, sysadmin
s.kalaivanan
Advisor

Re: cron Entry not working

Thanks All ,

Hi Bill Hassell,

My responses in-line below

1.Not a good place to store anything permanent. Any user can erase this file or rename it.

>>>this is test server so users already know root passwd , so that ok

2....as mentioned, it runs from your login. Type the command env and you will see that there are a lot of variables defined because you logged in. cron does not login

>> I'm logging as a super user ( root user )

3. OK
4.ok file premission changed ( 700 )
>> -rwx------ 1 root sys 2082 Apr 22 18:20 fs.sh

set -x --> where i need to add , pls find the attached my script ,

This is script is running fine my manual
{ #cd /tmp/bdf
# sh fs.sh
} --> working fine , and get output as FSusage_27-Apr.txt file

by using cron is not working , pls help how to resolve this issues ,

Thanks , Kalai.
R.O.
Esteemed Contributor

Re: cron Entry not working

Hi,

I have tested your script and I've seen it works, but I had to manually enter the user/passwd when it connects to the hosts...
You might provide the user/passwd data to not be prompted for them.

Regards,
"When you look into an abyss, the abyss also looks into you"
Pete Randall
Outstanding Contributor

Re: cron Entry not working

As said before (multiple times, I might add), cron provides a minimal environment. As it says in the crontab man page:

"cron supplies a default environment for every shell, defining:

HOME=user's-home-directory
LOGNAME=user's-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh"

When you run the script manually, your own environment has many more variables set and a much more extensive PATH variable. Perhaps the easiest way to provide that sort of environment to your script would be to invoke your profile in the script, like this:

. ~/.profile

Make that the very first line of your script. You may want to substitute /etc/profile in place of .profile depending on how your system is set up to define environment variables.


Pete


Pete
V. Nyga
Honored Contributor

Re: cron Entry not working

Hi,

>set -x --> where i need to add

As Bill said:
The first line of your script should be:
#!/usr/bin/sh
The second:
set -x

Your sixth line:
rm FSusa* - Add the full path of all files!

Now you should get error messages with more informations, which you need.

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
s.kalaivanan
Advisor

Re: cron Entry not working

Hi R.O.
>> we need to generate rsa and
do >> cat .ssh/id_rsa.pub |ssh second_hostname 'cat >> .ssh/authorized_keys'

after that only first time will ask password

Hi V. Nyga

>> after added as you said still same response

not running cron job ,

Hi Pete Randall ,

>> pls I'm not getting you ,, i have already attached my script , can you help

Thanks in advance , Kalai
Dennis Handly
Acclaimed Contributor

Re: cron Entry not working

>I have already attached my script, can you help

As Volkmar said, you need to insert at the top of your script:
#!/usr/bin/sh
set -x

Then run this with cron and then attach your mail message.

Ah, you can't do this:
function CHECK_DISK_USAGE {
echo

This echo tells cron you are a bad guy and deserve a mail message.