How to Autostart GlassFish Server on Ubuntu Startup

Learn how to configure GlassFish Server to start automatically on system boot in Ubuntu. Step-by-step guide using systemd service scripts.

Auto start GlassFish Server on Ubuntu

If you want GlassFish Server to launch automatically when your Ubuntu system boots, you can easily achieve that by creating an init script. This allows you to manage GlassFish’s start, stop, and restart actions seamlessly.

In this article, we learn to create the init script for GlassFish server and how to configure it in Ubuntu systems to control GlassFish server.

Step 1: Create the Init Script

The init script file for GlassFish Server is to be created at /etc/init.d/.

For managing all GlassFish Server startup events, it ships with the asadmin tool. Use this tool in the startup script as follows,

Create GlassFish init file using the following command:

sudo nano /etc/init.d/glassfish

Paste the following lines in the file

#!/bin/sh
# Prevent potential issues by defining Java path
export AS_JAVA=/usr/lib/jvm/jdk1.8.0
GLASSFISHPATH=/home/glassfish/bin

case "$1" in
  start)
    echo "Starting GlassFish from $GLASSFISHPATH"
    sudo -u glassfish $GLASSFISHPATH/asadmin start-domain domain1
    ;;
  stop)
    echo "Stopping GlassFish from $GLASSFISHPATH"
    sudo -u glassfish $GLASSFISHPATH/asadmin stop-domain domain1
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 3
    ;;
esac

This script uses asadmin to control the domain named domain1, running it as a dedicated glassfish user for security and proper permissions.

Step 2: Make the Script Executable

Now, glassfish startup script is created. We need to add this file in startup to make Glassfish Server autostart during Ubuntu startup.

Set the appropriate permissions so the system can run it at boot:

sudo chmod a+x /etc/init.d/glassfish

Step 3: Register the Script to Run at Startup

Link it into Ubuntu’s init system to execute during startup:

sudo update-rc.d glassfish defaults

This ensures the script will be triggered automatically during system boot.

Step 4: Validate the Setup

Now, restart Ubuntu and check if it really autostart the Glassfish Server.

Step 5: Validate Manual Commands

You can also manage Glassfish Server startup events as follows,

sudo /etc/init.d/glassfish start  # Start the server
sudo /etc/init.d/glassfish stop   # Stop the server
sudo /etc/init.d/glassfish restart  # Restart the server

Conclusion

Setting up GlassFish to start automatically on boot ensures that your applications and services are always available after a system reboot—without requiring manual intervention. By creating a simple init script and registering it with Ubuntu’s startup sequence, you can streamline your server management and reduce downtime. This method is especially useful for production environments where stability and automation are critical.

If you’re using a newer Ubuntu version that defaults to systemd, consider switching to a systemd service file for even better control and logging.

Comments

comments