How to Configure Apache Tomcat as a Startup Daemon on macOS (Auto-restart and Crash Recovery)


3 views

When installing Tomcat via MacPorts on macOS, the service doesn't automatically register itself as a launchd daemon - the modern replacement for System V init scripts in macOS. This means we need to manually create a proper plist file in /Library/LaunchDaemons to achieve:

  • Automatic startup during system boot
  • Proper process supervision (auto-restart on crashes)
  • Correct environment variable loading
  • Proper user/group permissions

First, create a new plist file with appropriate permissions:

sudo nano /Library/LaunchDaemons/org.apache.tomcat.plist

Here's a comprehensive template (adjust paths for your Tomcat version):





    Label
    org.apache.tomcat
    ProgramArguments
    
        /opt/local/share/java/tomcat6/bin/catalina.sh
        run
    
    RunAtLoad
    
    KeepAlive
    
    WorkingDirectory
    /opt/local/share/java/tomcat6
    StandardOutPath
    /var/log/tomcat/catalina.out
    StandardErrorPath
    /var/log/tomcat/catalina.err
    EnvironmentVariables
    
        CATALINA_HOME
        /opt/local/share/java/tomcat6
        JAVA_HOME
        /Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home
    

After creating the plist file, execute these commands:

sudo mkdir -p /var/log/tomcat
sudo chown -R _www:_www /var/log/tomcat
sudo chmod 755 /Library/LaunchDaemons/org.apache.tomcat.plist
sudo launchctl load -w /Library/LaunchDaemons/org.apache.tomcat.plist

To verify the service is running:

ps aux | grep tomcat
sudo launchctl list | grep tomcat

For troubleshooting, check the log files:

tail -f /var/log/tomcat/catalina.out
journalctl --since "1 hour ago" | grep tomcat

For production environments, consider adding these additional parameters to the plist:

ProcessType
Interactive
AbandonProcessGroup

LowPriorityIO

Nice
1

To enable SSL/TLS with automatic reload:

WatchPaths

    /opt/local/share/java/tomcat6/conf/server.xml

Before setting up Tomcat as a daemon, ensure you have:

  • macOS system with admin privileges
  • MacPorts installed (sudo port selfupdate first)
  • Java Development Kit (JDK) properly configured

For Tomcat 9 (current LTS version):

sudo port install tomcat9
sudo port load tomcat9

Verify installation:

curl http://localhost:8080
# Should return Tomcat default page

Create a plist file at /Library/LaunchDaemons/org.apache.tomcat.plist:





    Label
    org.apache.tomcat
    ProgramArguments
    
        /opt/local/share/java/tomcat9/bin/catalina.sh
        run
    
    RunAtLoad
    
    KeepAlive
    
    StandardOutPath
    /var/log/tomcat/console.log
    StandardErrorPath
    /var/log/tomcat/error.log
    EnvironmentVariables
    
        CATALINA_HOME
        /opt/local/share/java/tomcat9
        JAVA_HOME
        /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
    

Set proper permissions and create log directory:

sudo mkdir -p /var/log/tomcat
sudo chown -R _tomcat:_tomcat /var/log/tomcat
sudo chmod 755 /Library/LaunchDaemons/org.apache.tomcat.plist

Enable and start the service:

sudo launchctl load -w /Library/LaunchDaemons/org.apache.tomcat.plist
sudo launchctl start org.apache.tomcat

Verify status:

sudo launchctl list | grep tomcat

For production environments, consider adding these JVM options in setenv.sh:

CATALINA_OPTS="-server -Xms512m -Xmx1024m -XX:+UseG1GC"
CATALINA_PID="/var/run/tomcat.pid"

Create the file at /opt/local/share/java/tomcat9/bin/setenv.sh and make it executable:

sudo chmod +x /opt/local/share/java/tomcat9/bin/setenv.sh

Common issues and solutions:

  • Port conflicts: Check sudo lsof -i :8080
  • Permission errors: Verify _tomcat user has access to all required directories
  • Java version mismatch: Update JAVA_HOME in plist file

For detailed logs:

tail -f /var/log/tomcat/error.log