Cron Script Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cron Script Question
# 1  
Old 07-03-2004
Cron Script Question

A co-worker is having trouble w/ a job scheduled from cron and I got involved. Unfortunately I couldn't seem to find the answer and need some help. First off, I'm pretty sure he's using redhat linux. Anyway, I want to schedule a cronjob to run a java class. Let's say that, logged in as root, I excute the java class like this...

java MyJavaClass > MyJavaClass.out

This works and the file MyJavaClass.out ALWAYS contains data. In cron I created this entry below to run this command at 5:30AM each day)...

30 5 * * * java MyJavaClass > MyJavaClass.out

HOWEVER, when cron kicks off the job the size of MyJavaClass.out is always 0 bytes. So it looks like the job kicked off at the right time but, for some reason, the call to "java MyJavaClass" didn't execute correctly. My gut feeling is that this is some weird scripting error where the pathing isn't set correctly inside this cron script but I can't figure it out. I've even tried putting in the full path name to the java command (/usr/bin/java or whatever it was) but it still comes back w/ zero bytes. Can anyone explain what's going on to me? Thanks in advance!
# 2  
Old 07-03-2004
The problem seems to be with the location from which cron executes "java MyJavaClass". It seems like the class MyJavaClass.class cannot even be found in the classpath, so the NoClassDefFoundError exception is thrown. There is no output as the error is sent to standard error so your MyJavaClass.out becomes truncated.

What you need to do is to explicitly put a "cd" in the cron line to change the CWD before invoking java, like

cd /some/dir && java MyJavaClass > MyJavaClass.out

You cannot qualify the class with any path, as you probably know, since java expects a class name (optionally with package) as argument which at most only has dots denoting package, not a path with slashes.
# 3  
Old 07-03-2004
Follow up questions

Thanks for answering but as is par for the course for me, I have more questions. Smilie

1) The thing that bugs me is that when I run the command "java MyClass" from the command line while logged in as root it runs fine. That means that "MyClass" must be in the CLASSPATH. If so, then I have to assume that the CLASSPATH is changing inside the cron script. I have no idea why though and could sure use an explanation. In fact, I guess I can't be sure that ANY environment variable is what I think it is for that matter. So, it's possible that the PATH variable is set wrong too.

Is there any way to tell what these settings are inside the script? Is the best way to figure that out to write a script that says "env > ~/myEnv" and see what the results are? Or is there a better way?

2) How do I redirect both stdout and stderr of that script to a file? Is it "java MyClass 2&1> ~/myLogfile"? Also, is there any way to put the stdout in one logfile and the stderr in another file?
# 4  
Old 07-03-2004
1. I think you misunderstood my point. So, have you tried out what I suggested and did that work for you? I think it should, otherwise it may mean you need to explicitly specify the classpath in the java command, which is tedious and I will avoid it if ever possible because, depending on the Java project, missing some Java library class paths that exist in the default classpath will render those libraries unloadable.

No, the Java classpath has not changed. Unless you explicitly specify the classpath, the default classpath used by java ALWAYS contains the current directory (.) that's why if you put a Java class that is not in any package (see Java books for "package" if you don't know what Java packages (i.e. namespaces) are and how they work) in the current directory Java can always find it because . is in the classpath. But when you run it from cron, the current directory is not where your class resides, so it cannot be found. That was the point I would like to make in my previous reply.

PATH has nothing to do with class invocation in java UNLESS you install java in non-standard locations. You can qualify java with absolute path but as you said previously it did not work either. Therefore, it seems like Java can be loaded, it just couldn't find the class. Java doesn't use PATH to do anything internally except from to load the java executable itself.

2.
To same file:
java MyClass >~/myLogfile 2>&1

To different file:
java 1>~/stdout.log 2>~/stderr.log

But if for use with cron, I recommend avoid using ~ but to use absolute paths for filenames.

Last edited by cbkihong; 07-04-2004 at 12:20 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Execution problem with Cron: Script works manually but not w/Cron. Why?

Hello gurus, I am making what I think is a simple db2 call from within a shell script but I am having difficulty producing the desired report when I run the script shown below from a shell script in cron. For example, my script and the crontab file setup is shown below: #!/bin/ksh db2... (3 Replies)
Discussion started by: okonita
3 Replies

2. UNIX for Dummies Questions & Answers

cron shell script question

Hi all, hopefully someone out there can give me some tips on how to resolve this I have a simple shell script who -u > /tmp/userlog one of the line stored in userlog when executing shell script manually: jim123 pts/24 2012-03-30 13:02 00:56 4131 (xya.dsfgds.com) one of the... (4 Replies)
Discussion started by: Linux_Novice
4 Replies

3. Shell Programming and Scripting

Question regarding the cron

Hi, We have developed the script which will send an (html)attachment in through mail. The stand alone script is working fine ,but when we schedule it cron the "nail" command is not working that is we are not receving any mail. Following is the command which we used to send the mail. :confused:... (4 Replies)
Discussion started by: Amey Joshi
4 Replies

4. UNIX for Dummies Questions & Answers

Running a script in cron question

There is this script I'd like to put into cron, but it asks for date verification. It'll prompt you to press enter to continue. Usually, 100% of the time the dates are ok, so is there a way to run this script in cron and bypass the "enter" prompt? (3 Replies)
Discussion started by: NycUnxer
3 Replies

5. Shell Programming and Scripting

cron question

folks; This might sounds stupid, but i tried few ways to solve it without luck. I need to run a job on the first Saturday of every month at 10 pm. so far i'm not sure what am i doing wrong, but every time i set it, it always run every Saturday instead of the first Saturday of every month. Any... (4 Replies)
Discussion started by: moe2266
4 Replies

6. Shell Programming and Scripting

cron question

Folks; I have this script in SUN which if i run it using command line, it works fine, but when i run it using cron, it work but it misses one thing. here's the details #!/bin/bash cd /opt/new_script for i in ./report*Groups.sh; do $i $1; done This script above when runs through cron, it... (4 Replies)
Discussion started by: moe2266
4 Replies

7. UNIX for Dummies Questions & Answers

question about cron

I have a script which goes out and installs a package on a box remotely and im trying to get it to add a cron job too..but im having a few problems, whilst it does add the entry it wont run unless I connect to the machine , crontab -e, then :wq. This re-reads the configuration file and it will now... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

8. AIX

CRON Question

Could someone please tell me how I might specify in cron that I want a job to run only on the "last saturday" of the month? Thanks. (2 Replies)
Discussion started by: DenD
2 Replies

9. Shell Programming and Scripting

cron question

I'm new to cron, What does this syntax mean? 0 5 * * 1-5 /u08/0ra33/run_arch.sh thanks (2 Replies)
Discussion started by: ted
2 Replies

10. UNIX for Dummies Questions & Answers

Question about Cron jobs?

I can see where the nohup command can come in very handy. My question is, do you have to do something like this (nohup) in order to run certain cron jobs? On the windows side, I have a couple of scripts that basically run continuously, so all I have to do is ctrl-alt-del to lock my workstation,... (2 Replies)
Discussion started by: wmosley2
2 Replies
Login or Register to Ask a Question