0

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" [email protected]
        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
1
  • Which crontab>?
    – dunxd
    Feb 20, 2013 at 12:47

2 Answers 2

3

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.

4
  • 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, 2013 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, 2013 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?
    – replay
    Feb 21, 2013 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, 2013 at 4:05
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .