So as the title states, Ive got an issue with Crontab and my Java program connecting to a remote MySQL database.
The program itself is quite simple: it connects to the database, grabs two tables (users & passwords), then assigns that to a temporary variable and pushes the variable output to a shell script that creates a new user w/ home directory and a password.
The program itself works when run manually, however when I try to run the program through Crontab, it fails to connect to the database. Now, I have tried a variety of different things to get this to work such as assigning the crontab to different times in the day (we have other programs accessing the database as well, so I assigned it a time when the others didnt.), but that didn't work and just gave me the same issue. I have tried running the Java program directly from crontab, through a script assigned to crontab, and through a script that activates the previous script... and each method fails. (The scripts run, but again - no DB connection is made).
I am pulling my hair out at this point trying to figure this out... It works when I manually run it, yet fails when crontab tries to run it... (WTF?)
Furthermore, Crontab seems to run fine as it executes all the items in the list, but when it gets to my Java program, the connection is never made for some reason...
So if anyone has a clue as to what may be happening here, I would greatly appreciate the input.
Also, if anyone can tell me how to print the error output of the SQL connection through Java, it may help me / us to diagnose the issue...
Thanks again!
Additional information:
Running Ubuntu 4.4.3
Latest version of linux JDK from apt-get
Latest version of linux Java MySQL integration from apt-get
I do *NOT* have MySQL installed locally! everything is remote.
Crontab line used to execute java program:
30 14 * * * java -cp $CLASSPATH:/home/user/java myprogram > /home/user/java/user_logfile.txt
Classpath:
/usr/share/java/mysql-connector-java-5.1.10.jar
Updated Information:
Using Jukka's advice about the printStackTrace, I looked up on google for a method to send the stacktrace to the System.out.println function so crontab could more accurately give me a log digest of what was going on. I managed to find:
catch(Exception e)
{
ByteArrayOutputStream file = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(file);
e.printStackTrace(stream);
System.out.println("The result is :"+file.toString().trim());
}
which helped me to find this:
Error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at prog_users.main(prog_users.java:24)
So the main error is that the classpath for crontab cannot find the jdbc driver!
Now... how do I change the CLASSPATH for a process that has no home directory or .rcbash...?