issue invoking shell script using cron, even with proper file permission

I am using tcsh

what could possibly be a problem, when using crontab to invoke a shell script. ?

The script has the read, write and execute permission to all users. And the script works as expected while executing it in stand-alone mode.

Is there a way to trace (like log) what error cron scheduler faces while trying to invoke the shell script ?

Myscript.ksh
#!/bin/ksh
`echo date >ech.txt`

my cron entry
21 11 30 * * /pkgs/roots/scripts/Myscript.ksh

you are missing absolute path for the file where you are redirecting.

you should give something like,

>/tmp/output

You're not using tcsh. You're using ksh.

Errors normally go to the mailbox of whoever runs the job.

Perhaps you could say /bin/date, instead of just date.

(as well as thegeek's suggestion about the output path)

i tried giving absolute path for redirection. But it didn't work.

i also tried to giving the cron entry
21 11 30 * * /pkgs/roots/scripts/Myscript.ksh 2>&1 > /pkgs/roots/scripts/tempfile.txt

However i just got an empty file(tempfile.txt) created at the scheduled time.

Why are you actually doing this:

`echo date > ech.txt`

when

date > ech.txt

would do?

Date is quite capable of executing and echoing itself without help (unless you mean to echo the literal string "date" to a file, that is)

in which case

echo date > echo.txt

would suffice.

The backticks are not required in this context and cause the script to be unpredicatable whether run from the command line or from cron. Also the "echo" is probably not required if you want to get the current date into file ech.txt.

#!/bin/ksh
date > ech.txt

From your later post.

The script does not output anything which is why tempfile.txt is empty. Any syntax errors etc. will be in unix mail for the user issuing the cron.

The cron will of course only execute when it is 11:21 on the 30th of the month.

Hi All,
Thanks a lot for all your help.

I tried to invoke Myscript from another script. And i put the calling script(call_Myscript.ksh) in the cron. But in the log(tempfile.txt) i jus get the echo before the script invocation only. None of the echo in Myscript.ksh
goes to the log. both script file permissions are 777.

Can u please help me find what's wrong in this..?

Script 1: /pkgs/roots/scripts/Myscript.ksh

 #!/bin/ksh
 echo "script execution starts" >> tempfile.txt
 DAY=`date +%j`
 echo "JUL_DT is $DAY" >> tempfile.txt
 echo "script execution completes" >> tempfile.txt

Script 2: /pkgs/roots/scripts/call_Myscript.ksh

 #!/bin/ksh
 echo "calling the script" >> tempfile.txt
 /pkgs/roots/scripts/Myscript1.ksh
 echo "script completed successfully" >> tempfile.txt

cron entry

55 07 30 * * /pkgs/roots/scripts/call_Myscript.ksh 2>&1 >/pkgs/roots/scripts/tempfile.txt 

what is the proper way to call a script from another script ?

Either echo the output from your scripts in the scripts themselves, or from the cron job. But not both.

You should also try to fully qualify the logfile you are writing to or the script you are calling instead of just using the filename on its own, escecially important when you expect to run the scripts using cron or other scheduling programs.

And please use code tags. That makes it easier for everyone to read your post.

again, use absolute path.

  echo "script execution starts" >> tempfile.txt

to

  echo "script execution starts" >> /tmp/tempfile.txt

Note the /tmp/ in the above. Got it ?

The first thing you should do is source your .profile in call_Myscript.ksh

cron does NOT automatically source the users profile.

After that, you should do as several others have said. Fully qualify commands and files with paths. You can use variables to do this to simplify things when you need to make changes.

#!/bin/ksh

#
# source the profile.
#
# Note that there is a space between the 1st "." and the tilde "~"
#

. ~/.profile

#
# Define a path to my output files.
#
OUTDIR=/dir1/dir2/output
OUTFILE=${OUTDIR}/tempfile.txt

#
# Echo the date to the file
#
# Note that ">>" will always append to a file. If you want to overwrite the file use ">"
#
date >> ${OUTFILE}