aspose file tools*
The moose likes Linux / UNIX and the fly likes crontab Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "crontab" Watch "crontab" New topic
Author

crontab

chennai Kumar
Ranch Hand

Joined: Jun 04, 2005
Posts: 72
Hai All,
The following sh file is running fine when i run manually, but the same sh file is configured in corntab its not executing daily.

my sh file:


This is my cron job


els_batch.sh file has all permission, and checked the path also its correct.

please help me to fix this problem.

Thanks in advance.

Thanks and Regards,
Abiramkumar.P

[Andrew's edit: Break CLASSPATH over multiple lines so that this page is readable. Put code inside UBB Code tags]
[ July 03, 2008: Message edited by: Andrew Monkhouse ]
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Abiram,

do you execute this script manually with the same user to which this crontab belongs?

Besides this you could try to give the full path to your shell (/bin/sh or /usr/bin/sh?) because depending on your environment configuration the cron scripts don't have the same PATH environment variable you have when executing things interactively.

And it's always a very good idea to take a look at the syslog file on UNIX/Linux systems. At the specified time when the cron job should execute there must be an entry what it does or what went wrong, so just have a look at it

Marco
chennai Kumar
Ranch Hand

Joined: Jun 04, 2005
Posts: 72
Thanks for reply Marco.

I executed script manually with the same user to which this crontab belongs.
Script is try to running from user root directoy, even if i have to configure bin/sh.
If i am run in solaris environment where can i see specified time is log file is running or not.

Thanks in advance,
Thanks and Regards,
Abiramkumar.P
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11503
    
  95

Hi Abiram,

As Marco suggests, you should probably be explicit as to which shell you want to run your scripts. From memory, on Solaris scripts will be run by /usr/bin/sh if you do not explicitly specify anything else. While that should be fine in this case, it is easy to be logged in using a different shell (tcsh, csh, ...) and write a viable script that then fails at deploy time in weird ways.

So, even if you are happy with running using the standard shell, I would still recommend your first line be #!/usr/bin/sh just so it is explicit to anyone looking at your code that this is what you wish to use.

Script is try to running from user root directoy,

Correct. By default, all cron and at scripts are run from the home directory of the user who owns the crontab or at entry.

I suspect that if you look in the DebugUpload.log file, you are probably getting a message about the class not being found. I further suspect that the DebugUpload.log file is in the user's home directory (and I bet that is not where you expected it)

So that then gives a likely fix: Around line 2 or 3 of your script, change to the directory where you really want the script to run. Something like:



If you are still having troubles, then please tell the details of the errors. There should be something in the DebugUpload.log that will show what the error is.

Speaking of the DebugUpload.log file - instead of creating that in the script you are running, why not create it in the crontab entry (and give it a fully qualified path). At the moment I am betting on this file being in the user's home directory, but I am not sure. If the log file was specified using a fully qualified path then we would know where the file was. Likewise, if the log file was being created from the crontab entry then you would also see any errors in the cron running or any errors in the script that were external to Java.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
chennai Kumar
Ranch Hand

Joined: Jun 04, 2005
Posts: 72
Thanks for your Reply Andrew,

One of my friend has asked to add " #!/bin/sh " in the first line of shell script.Is this correct or i have to add "#!usr/bin/sh".

Thanks and Regards,
Abiramkumar.P
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11503
    
  95

Type which sh and it should tell you which is the correct location. For example, when I try this (on my non Solaris system), I get:



So if I was building a script to work on my laptop, I would use /usr/bin/sh. The exact location on your Solaris installation may be different though.

Regards, Andrew
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Abiram,

if you give an explicit shell when executing your script like with "/bin/sh skriptname.sh" it works even without including the interpreter in the script. So for you crontab it should even work without the #!/bin/sh or #!/usr/bin/sh.

As Andrew already said the problem is surely either that cron doesn't find your shell in its path or that you are in the wrong directory when the script is executed. But this should show up in the system logs or in the log generated by your script. There must be some error message at the time the cron job runs.

Marco
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16233
    
  21

Actually, it might be better to do the following:

1. Add the "shebang" to the script. I think "#!/bin/sh" is pretty universal - often linked to /usr/bin/sh, though WHICH shell you get may be unpredictable. The default one in Solaris is the dumb one. If you need a specific shell, use its absolute path.

2. Make sure that the script is marked as executable. This is CRITICAL!

3. When using selinux, make sure that the SE attributes are suitable for cron.

You should now be able to remove the "sh" command from your crontable entry and actually this is better, since if your script needed a specific shell such as csh or ksh, the script itself would be able to ensure it was used.

In Solaris, check the /var/adm directory for log info. /var/log doesn't always have what you need.


Customer surveys are for companies who didn't pay proper attention to begin with.
chennai Kumar
Ranch Hand

Joined: Jun 04, 2005
Posts: 72
Friends,

Thanks for all replys!!!

Still i have a problem in production server.I have an one doubt,Please clarrify.

When cronjob runs, PWD shows following path.

code:
________________________________________
data/userhome/wmsuser/
________________________________________


My main class is in Datasync.jar, i set in CLASSPATH.My modified sh file as below,

code:
________________________________________

#!/usr/bin/sh
#
JAVA_HOME=/data/bea/jdk141_05

PROJECT_DIR=/data/bea/user_projects/domains/mydomain/applications/projectname/DataSynch/DataSynch_Internet_Upload

LIB_HOME=$PROJECT_DIR/lib

CLASSPATH=$LIB_HOME/DataSynch.jar:$LIB_HOME/weblogic.jar:$LIB_HOME/mail.ja:$LIB_HOME/ojdbc14.jar:$LIB_HOME/activation.jar:$LIB_HOME/classes12.jar:$LIB_HOME/commons-net-1.4.1.jar:$LIB_HOME/xom-1.1.jar:$LIB_HOME/log4j-1.2.8.jar:$LIB_HOME/jakarta-oro-2.0.8.jar:$LIB_HOME/cglib-nodep-2.1_3.jar:$LIB_HOME/dom4j-1.6.1.jar:$LIB_HOME/jaxen-core.jar:$LIB_HOME/jaxen-jdom.jar:$LIB_HOME/jdom.jar:$LIB_HOME/joda-time-1.2.1.jar:$LIB_HOME/junit.jar:$LIB_HOME/saxpath.jar:$LIB_HOME/stax-1.2.0.jar:$LIB_HOME/stax-ap i-1.0.1.jar:$LIB_HOME/xalan.jar:$LIB_HOME/xerces.jar:$LIB_HOME/xml-apis.jar:$LIB_HOME/xml-writer-0.2.jar:$LIB_HOME/xom-1.1.jar:$LIB_HOME/xpp3_min-1.1.3.4.O.jar:$LIB_HOME/xstream-1.2.1.jar:$LIB_HOME/xstream-benchmark-1.2.1.jar

export CLASSPATH

PATH=$JAVA_HOME/jre/bin:$JAVA_HOME/bin:$PATH:.

export PATH

#
#
cd /
cd $PROJECT_DIR/runtime

java com.qs.datasync.DataSyncFtpUpload 1>> DebugUpload.log

________________________________________


When run manually in runtime folder it is running fine.So i added following two lines in sh file.

code:
________________________________________


cd /
cd $PROJECT_DIR/runtime

________________________________________


When cronjob run it goes to " data/userhome/wmsuser/ " So i used cd / to travell to root directory and moved to runtime package. This is right or how to give correct path.Please correct me.


Please help me to solve this problem.

Thanks and Regards,
Abiramkumar.P
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Abiram,

from my perspective your new script looks fine except for one detail...

cd /
cd $PROJECT_DIR/runtime

This is OK. You don't even need the "cd /" because $PROJECT_DIR is already an absolute path. It will definetly use the right path.

java com.qs.datasync.DataSyncFtpUpload 1>> DebugUpload.log

I think, this could make you trouble. As you only export your CLASSPATH and JAVA_HOME variable chances are good that this interferes with other settings in your system at runtime. Better use the variables explicitly, for example:

Hope this helps. At the moment I don't see any other issues. Doesn't your log file show any hints?

Marco
chennai Kumar
Ranch Hand

Joined: Jun 04, 2005
Posts: 72
Dear Friends,

Thanks for all you reply.

Finally i solved this issue.My boss asked to put the following code in my sh file.

Now it is working fine. This path is copied from user profile.

Regards,
Abiramkumar.p
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: crontab