Date command does not work in a KSH when called from Cron


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date command does not work in a KSH when called from Cron
# 1  
Old 04-30-2009
Question Date command does not work in a KSH when called from Cron

Hi,

I'm trying to execute a job on the last day of every month, for which i'm using the following code in my Korn shell script :

if [[ `date +%d -d tomorrow` != 01 ]];
then
echo allowed only on last day of month. Today is `date +%d` >t.stm
echo Tomorrow is `date +%d -d tomorrow` >s.stm
exit 0
fi
...
....
.... (actual code to be executed)


Here is the crontab entry for it :

2,32 6-10 28-31 * * /intl_cim/acq_mdl1/cms/prod/global/scripts/trigger.ksh prod > /dev/null 2>&1

Strange enough, when i execute this script from the command prompt it works fine, but when it is executed by the cron, the file s.stm and t.stm show the same date. Which means it is not calculating Date -d tomorrow.

Can you please help me understand why this would happen. Here is what uname -a on my machine says :

SunOS sppmd541 5.10 IDR_139180-01 sun4u sparc SUNW,Sun-Fire-15000

Thanks a lot.
# 2  
Old 04-30-2009
Solaris' "/usr/bin/date" does not support '-d tomorrow' option - this is GNU-ism.
You must have the GNU 'date' installed under /usr/local/bin (most likely) and your $PATH env variable has /usr/local/bin BEFORE /usr/bin.
Try specifying the the FULL path to the GNU date in your script.
Also Solaris' crontab does not support the 'day-day' or 'hour-hour' syntax - this is also GNUism - see 'man crontab' for description.
For further cron/crontab issues refer to the crontab FAQ.

Last edited by vgersh99; 04-30-2009 at 10:34 AM..
# 3  
Old 04-30-2009
When using shell scripts within cron, it is always a good idea to hard code the full path to each and every utility i.e. /usr/bin/cp, /usr/local/bin/date, etc.
# 4  
Old 04-30-2009
On a HP-UX system anything after the format string in the unix date command is ignored. Though this is SunOS, it might explain why the script appears to work without error messages.

Code:
date +%d fred bloggs
30

date +%d -d tomorrow
30

BTW. Solaris crontab does support 'day-day' 'hour-hour' syntax.
# 5  
Old 04-30-2009
Quote:
Originally Posted by methyl
On a HP-UX system anything after the format string in the unix date command is ignored. Though this is SunOS, it might explain why the script appears to work without error messages.

Code:
date +%d fred bloggs
30

date +%d -d tomorrow
30

BTW. Solaris crontab does support 'day-day' 'hour-hour' syntax.
I stand corrected - I thought of 'every' syntax: every 5 minutes: '*/5'
# 6  
Old 05-02-2009
But the script works correctly from the command line, and the date command is executed as expected. e.g. if i run it from command line on 30th april, the condition Date +%d -d tommrrow != '01' is false and script executes further. But when it is called from the cron on 30th april, this condition becomes true because 'date +%d -d tommorrow' returns 30 instead of 01.


Why would the same command that returns 01 on the command line will return 30 when called from cron ?

Quote:
Originally Posted by methyl
On a HP-UX system anything after the format string in the unix date command is ignored. Though this is SunOS, it might explain why the script appears to work without error messages.

Code:
date +%d fred bloggs
30

date +%d -d tomorrow
30

BTW. Solaris crontab does support 'day-day' 'hour-hour' syntax.
# 7  
Old 06-02-2009
Please show the exact SunOS command which produces tomorrow's day number. The posts include three spellings of the word "tomorrow", but I don't think that SunOS is interpreting anything on the command line after
Code:
date +%d


Last edited by methyl; 06-02-2009 at 06:59 PM.. Reason: Typos
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Commands to call script work from command line but not from Cron entry

My first post evidently did not materialize so I posted it again: Runnning a cron job every 5 mins to send data files to a state facility. My original cron entry at worked fine: 01,06,11,16,21,26,31,36,41,46,51,56 * * * * /home/sftpuser/stateinoc-from-appname.ksh Somewhere I have a... (1 Reply)
Discussion started by: Skyybugg
1 Replies

2. Shell Programming and Scripting

Cron Job - date command problem

Hi I have one problem with cron job in Control Panel. I have a log file that is created once a day on another server and I need to transfer it in an exact time to my server so I wrote the cron job for it BUT the problem is in the date command: /filelog-`date +%Y-%m-%d`.tar.gz; The file... (1 Reply)
Discussion started by: bobo_balde
1 Replies

3. UNIX for Dummies Questions & Answers

Interpreting Shell Script errors when called from CRON

Hi All, I am calling a series of shell scripts via CRON so everything is running as root. However, in my error log file I am seeing the following errors. Please can anyone offer any advise as to the possible causes and solution to prevent the errors from appearing. The Error 1227 seems to... (2 Replies)
Discussion started by: daveu7
2 Replies

4. Linux

Why service (start|stop) can not be called from cron?

Hi, When I googled, I came to know that I can not call the command "service <service-name> start" from cron, insted I have to specify path to the service-name. I wanted to know the reason. Thanks, Hansini (3 Replies)
Discussion started by: hansini
3 Replies

5. Shell Programming and Scripting

Script not working when called by cron

Hello, I have the following script which works fine when ran from the command line: #!/apps/python/2.3.4/bin/python import os import sys import time user = os.getenv("USER") string = time.strftime("%m%d%y0000 " + user, time.gmtime()) However, when I have this run by crontab, I... (4 Replies)
Discussion started by: cooldude
4 Replies

6. Shell Programming and Scripting

want to get previous date from date command in ksh

I want to get previous date from date command. I am using ksh shell. Exmp: today is 2008.09.04 I want the result : 2008.09.03 Please help. Thanks in advance. (4 Replies)
Discussion started by: rinku
4 Replies

7. Shell Programming and Scripting

Need help with ksh script that uses sqlplus, called from PHP

I have a ksh script that connects to sqlplus and dumps the query results into a file. The script works file when I run it from the command line, however, when I call it from PHP using system(), exec(), or shell_exec() commands, the script doesn't seem to run the query. It will create the text file... (7 Replies)
Discussion started by: j2owilson
7 Replies

8. Shell Programming and Scripting

Command doesn't work under cron ?

I have defined in cron crontab -l >/FIXENGINE/data/crontab.$(whoami).$(date +%a) The resulting file is named crontab.fixadmin. When I run the above from a command line, the file is named: crontab.fixadmin.Tue (or whatever day I happen to be running) The question is - why do I get... (4 Replies)
Discussion started by: bigjohn-nj
4 Replies

9. Shell Programming and Scripting

How to determine the script is called from CRON?

Hello expert, What I want is to determine whether the script is called from CRON or it is executed interactively? I tried the following but no luck: #!/bin/ksh cronID=`pgrep -x cron` GPID=`ps -ef -o ppid,pid | grep " $PPID$" | awk '{print $1}'` if ; then echo I am being run... (15 Replies)
Discussion started by: wes_brooks
15 Replies

10. Shell Programming and Scripting

gzip in shell script called by cron

I'm puzzled by this one. I hope you can explain it to me. I have a ksh shell script that gzips a file among other things. This works perfectly fine when the script is manually run through a shell. However, when the same script is run through cron, it does everything correctly, but it will... (2 Replies)
Discussion started by: hbau419
2 Replies
Login or Register to Ask a Question