The purpose of this post is to document how to start GlassFish 4 automatically whenever the server it is installed in reboots.
The VPS that is hosting my Oracle APEX installation rebooted without warning the other day (as should be expected of cheap services), which required me to personally SSH into the server to restart GlassFish.
The prospect of having to manually restart GlassFish every time the server reboots does not look appealing to me. I need to find a way to make GlassFish start automatically on boot.
Using GlassFish create-service command
Run the command
The GlassFish 4 Administration Guide Page 3-13 clearly recommends the use of the built-in command called create-service in order to automatically start GlassFish when the server boots up.
Let’s try and see it in action.
[root@server ~]# $GLASSFISH_DIR/bin/asadmin Use "exit" to exit and "help" for online help. asadmin> create-service Found the Linux Service and successfully uninstalled it. The Service was created successfully. Here are the details: Name of the service:domain1 Type of the service:Domain Configuration location of the service:/etc/init.d/GlassFish_domain1 User account that will run the service: root You have created the service but you need to start it yourself. Here are the most typical Linux commands of interest: * /etc/init.d/GlassFish_domain1 start * /etc/init.d/GlassFish_domain1 stop * /etc/init.d/GlassFish_domain1 restart For your convenience this message has also been saved to this file: $GLASSFISH_DIR/glassfish/domains/domain1/PlatformServices.log Command create-service executed successfully. asadmin> exit Command multimode executed successfully.
Attempt to start GlassFish service
It seemed that the command completed successfully. Now let’s try to start the service.
[root@server ~]# /etc/init.d/GlassFish_domain1 start [root@server ~]# /etc/init.d/GlassFish_domain1: line 54: $GLASSFISH_DIR/glassfish/lib/nadmin: Permission denied
Something is wrong in the script somewhere. Let’s examine the contents of the init script created by the create-service command.
[root@server ~]# vi /etc/init.d/GlassFish_domain1
Review lines 50 to 64:
ASADMIN="$GLASSFISH_DIR/glassfish/lib/nadmin" case "$1" in start) $ASADMIN start-domain --domaindir $GLASSFISH_DIR/glassfish/domains domain1 & ;; stop) $ASADMIN stop-domain --domaindir /$GLASSFISH_DIR/glassfish/domains domain1 & ;; restart) $ASADMIN restart-domain --domaindir $GLASSFISH_DIR/glassfish/domains domain1 & ;; *) echo "usage: $0 (start|stop|restart|help)" esac
The path to the asadmin command is wrongly set in the script. The correct path should be $GLASSFISH_DIR/bin/asadmin. Changing this to the correct path should rectify the error.
Modify the script
Change line 50 as follows:
Start GlassFish service
The GlassFish server can now be started using the modified script.
[root@server ~]# service GlassFish_domain1 start [root@server ~]# Waiting for domain1 to start ......................... Successfully started the domain : domain1 domain Location: $GLASSFISH_DIR/glassfish/domains/domain1 Log File: $GLASSFISH_DIR/glassfish/domains/domain1/logs/server.log Admin Port: 4848 Command start-domain executed successfully.
Restart GlassFish service
The GlassFish server can also be restarted by the script.
[root@server ~]# service GlassFish_domain1 restart [root@server ~]# Successfully restarted the domain Command restart-domain executed successfully.
Stop GlassFish service
Last but not least, the script can stop the GlassFish domain.
[root@server ~]# service GlassFish_domain1 stop [root@server ~]# Waiting for the domain to stop Command stop-domain executed successfully.
Start GlassFish 4 automatically on reboot
In addition to creating the init.d script, the asadmin create-service command also creates symbolic links inside the rc*.d folders, which makes GlassFish start automatically on runlevels 3,4 and 5.
Problems with the script
Although the script created by the asadmin create-service command is functional, I am not quite satisfied with it for the following reasons:
No chkconfig support
Other scripts in the init.d folder can be managed via the chkconfig command. You can set which runlevels you want to run your service in using this command. However, the script created using the asadmin create-service command is not compatible with chkconfig.
[root@server ~]# chkconfig GlassFish_domain1 --list service GlassFish_domain1 does not support chkconfig
No status checking
Other scripts in the init.d folder use the status operand to check the running status of the service. For instance, for the Apache web server:
[hnizam@server ~]$ sudo service httpd status httpd (pid 13365) is running...
Unfortunately, the script provided by the asadmin create-service command does not include the definition for the status operand.
[root@server ~]# service GlassFish_domain1 status usage: /etc/init.d/GlassFish_domain1 (start|stop|restart|help)
GlassFish runs under root user
Finally, the way the script created by the asadmin create-service command is written makes GlassFish run under the root user ID.
If you installed GlassFish under a non-root non-privileged user, and wants the GlassFish process to run under this user ID, the script will not do this for you.
To start GlassFish 4 automatically every time the server boots up, you can use the asadmin create-service command. However, on Centos 6 at least, the script created by the command will not work out of the box. Some modifications need to be done on the script before it will work as intended.