Bash – glassfish start script fails through crontab

bashcronglassfish

I have a created a script to check to see if my glassfish server is running, if it isn't, it attempts to kill the java process to ensure it's not hung, and then issues the asadmin start-domain command

If this script runs from the command line it is successful 100% of the time. When it is run from the cron tab, every line runs but the asadmin start-domain line does not seem to execute or at least does not complete, i.e. the server is not running after this script runs.

For anyone not familiar with glassfish or the asadmin utility used to start the server, it is my understanding that a forked process is used. could this be causing a problem via cron?

Again, in all my tests today, the script runs to completion when run from the command line. Once it's executed through the cron, it does not complete.

thanks in advance for any help… i'm pulling my hair out trying to make this work!

#!/bin/bash
timevar=`date +%d-%m-%Y_%H.%M.%S` 
process_name='java'
get_contents=`cat urls.txt`  

for i in $get_contents
do
echo checking $i
statuscode=$(curl --connect-timeout 10  --write-out %{http_code} --silent --output /dev/null $i)
case $statuscode in
    200)
        echo "$timevar $i $statuscode okay" >> /usr/home/user1/logfile.txt
        ;;
    *)
        echo "$timevar $i $statuscode bad" >> /usr/home/user1/logfile.txt
        echo "Status $statuscode found" | mail -s "Check of $i failed" some.address@mail.com
        process_id=`ps acx | grep -i $process_name | awk {'print $1'}`
        if [ -z "$process_id" ]
        then
            echo "java wasn't found in the process list"
        else
            echo "Killing java, currently process $process_id"
            kill -9 $process_id
        fi
        /usr/home/user1/glassfish3/bin/asadmin start-domain domain1
        ;;
esac
done

Also, just for completeness, here is the entry in the cron tab:

*/2 * * * *  /usr/home/user1/server.check.sh >> /usr/home/user1/cron.log

Best Answer

Is it possible that inside the cronjob the ${JAVA_HOME} is not set?

Java always requires certain environment variables to be set in the environment. Maybe you set them automatically when you use your cli, but the cronjob doesn't have them. Try to do env on your cli, to see if there is some java related stuff like ${JAVA_HOME} and if so make sure the cronjob also has them. For example by adding them in the top of your script.