Cron Issue

Hey all! I'm working on setting up a script to run with cron. I have thus far been unable to get it to successfully complete. I'm hoping you gurus can lend me a hand.

This is my crontab, the transfer script works fine the backup will not. The backup however does work when I execute it from the command line.

SHELL=/bin/bash
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/data1/baminstall:/home/Admin/bin
HOME=/home/Admin

*/2 * * * *  /opt/data1/BAM/utils/backup.sh
*/13 * * * * /opt/data1/BAM/utils/transfer.sh

The Cron logs show that it is executing it without errors...

Aug  9 16:52:01 BAM_PNPT crond[13936]: (root) CMD (/opt/data1/BAM/utils/backup.sh)
Aug  9 16:54:01 BAM_PNPT crond[13980]: (root) CMD (/opt/data1/BAM/utils/backup.sh)
Aug  9 16:56:01 BAM_PNPT crond[14018]: (root) CMD (/opt/data1/BAM/utils/backup.sh)
Aug  9 16:58:01 BAM_PNPT crond[14064]: (root) CMD (/opt/data1/BAM/utils/backup.sh)

This is the script I am trying to run.

#!/bin/bash
#!/usr/bin/
set PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/data1/baminstall:/home/Admin/bin

java -classpath "MicroXML.jar:log4j.jar:fileIO.jar:logging-server.jar:br-server.jar:core-server.jar:ds-server.jar:postgresql-8.1-404.jdbc2ee.jar" \
org.mg.br.server.utils.BackupRestoreCli -action backup -xml BackupRestoreConfig.xml

Thank you in advance!

---------- Post updated at 10:54 AM ---------- Previous update was at 10:49 AM ----------

SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
USER=root
LS_COLORS=no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:
KDEDIR=/usr
MAIL=/var/spool/mail/root
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/data1/baminstall:/root/bin
INPUTRC=/etc/inputrc
PWD=/root
LANG=en_US.UTF-8
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SHLVL=1
HOME=/root
LOGNAME=root
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
_=/bin/env

These are my env variables..

In your script, there are a few issues that I see off the top of my head.

First, the second shebang (#!) will not be parsed and it serves no purpose that I can see.

Secondly, if you are executing this as a bash script, you should not use "set" you should use export.

Also, you may want to change your PATH but unless you are certain everything that you need will be called from your path, you may want, as a practice to do as follows:

export PATH=<your paths>:$PATH

Thanks for the reply! I've changed the script to this. Still no luck. Any other ideas?

#!/bin/bash

java -classpath "MicroXML.jar:log4j.jar:fileIO.jar:logging-server.jar:br-server.jar:core-server.jar:ds-server.jar:postgresql-8.1-404.jdbc2ee.jar" \
org.mg.br.server.utils.BackupRestoreCli -action backup -xml BackupRestoreConfig.xml

You forgot to export your path as suggested above. cron gets a much more minimal path than a shell login, so it won't be able to find certain programs until you set a better PATH yourself.

I tried it that way as well without success. I'm at a loss to why it isn't working..

The way you run your cronjob (without > /dev/null 2>&1) suggests that the actual output from your cronjob should be in your user's mailbox.

Check your mail on the server.

(as an aside, I don't think */13 notation transcends boundaries, so it will run at 0, 13, 26, 39, 42, 55, 0, 13, ..., etc, minutes past the hour, but I may be wrong :))

I've done the mail command and it tells me no mail on server :frowning:

check the cron settings for mail

# cat /etc/crontab |grep MAIL

and try

/usr/bin/java

instead of java

SHELL=/bin/bash
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/data1/baminstall:/home/Admin/bin
HOME=/root

*/2 * * * *  /opt/data1/BAM/utils/backup.sh
*/13 * * * * /opt/data1/BAM/utils/transfer.sh

I think it's all.

Okay, thank you for all the suggestions here guys. I did a bit more logging on this and here is what it is spitting out at me:

Exception in thread "main" java.lang.NoClassDefFoundError: /opt/data1/BAM/utils/backup/sh
Caused by: java.lang.ClassNotFoundException: .opt.data1.BAM.utils.backup.sh
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: /opt/data1/BAM/utils/backup.sh. Program will exit.

Any ideas what this could be?

Thanks!

URLclassloader? Is it trying to load /opt/data1/BAM/utils/backup.sh as a URL? Very strange. How do you usually run this command from the shell?
Give your script an identical path as you'd have on your login.

When I normally run this I just navigate to the directory, then execute backup.sh the script then runs without issues.

---------- Post updated 08-11-10 at 09:46 AM ---------- Previous update was 08-10-10 at 06:50 PM ----------

Thank you for the help everyone I was able to get it figured out. I changed the script the following lines way to get it to work

#!/bin/bash
export PATH=/opt/data1/BAM/utils:/opt/data1/BAM/bin:/usr/local/jdk1.6.0_16/bin:
JAVA_HOME="/opt/java/jdk"

cd /opt/data1/BAM/utils
java -classpath "MicroXML.jar:log4j.jar:fileIO.jar:logging-server.jar:br-server.jar:core-server.jar:ds-server.jar:postgresql-8.1-404.jdbc2ee.jar" \
org.mg.br.server.utils.BackupRestoreCli -action backup -xml BackupRestoreConfig.xml

Thanks again!