Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question
    
Which crontab>? –  dunxd Feb 20 '13 at 12:47
add comment

2 Answers

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.

share|improve this answer
    
I thought that might be it, but my glassfish installation is running on a freebsd system... the JAVA_HOME variable isn't set i ran the 'env' command and it wasn't displayed –  cotfessi Feb 21 '13 at 5:57
    
i tried setting the variable at the beginning of the script anyway and it didn't make a difference –  cotfessi Feb 21 '13 at 6:40
    
in your crontab you seem to have a log, you are redirecting the output to /usr/home/user1/cron.log. what does this log say when the cronjob runs? –  mauro.stettler Feb 21 '13 at 6:48
    
there are no errors there... it shows that the first time the process is found and killed, but no mention of the glassfish start, subsequent entries in cron.log show that the process does not exist, meaning that it never restarted –  cotfessi Feb 22 '13 at 4:05
add comment
up vote 0 down vote accepted

Ok... found the answer to this on another site, but I thought I'd add the answer in here for future reference.

The problem was that the path!! even though java_home was set, java itself wasn't in the path for the cron daemon.

A quick test to see what path is available to your cron, add this line:

*/2 * * * * env > /usr/home/user1/env.output

From what I can gather, the PATH initially available to cron is pretty minimal. Since java was in /usr/local/bin, i added that to the path right in the crontab and kaboom! it worked!

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

*/2 * * * * /usr/home/user1/server.check.sh >> /usr/home/user1/cron.log
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.