How to Programmatically Restart a Single TomCat 7 Web Application on Schedule in Windows


5 views

When managing TomCat 7 on Windows servers, administrators often need to restart specific web applications without affecting other deployed applications. The manual approach through TomCat Manager's "Reload" button becomes impractical for daily operations.

TomCat exposes its management functions through HTTP endpoints. We can automate the reload operation using simple HTTP requests:

HTTP GET /manager/html/reload?path=/yourwebappcontext

Here's how to implement this with cURL in Windows:

@echo off
curl -u admin:password "http://localhost:8080/manager/html/reload?path=/yourwebappcontext"

To execute this daily at a specific time:

  1. Create a batch file with the cURL command
  2. Open Task Scheduler and create a new task
  3. Set trigger to "Daily" with your preferred time
  4. Set action to "Start a program" and point to your batch file

For production environments, consider these security measures:

# In tomcat-users.xml
<role rolename="manager-script"/>
<user username="restartuser" password="complexpassword" roles="manager-script"/>

For more control, use JMX:

import javax.management.*;
import java.lang.management.*;
...
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("Catalina:type=Manager,context=/yourwebapp,host=localhost");
mBeanServer.invoke(objectName, "stop", null, null);
mBeanServer.invoke(objectName, "start", null, null);

Always implement proper error checking:

@echo off
curl -s -u admin:password "http://localhost:8080/manager/html/reload?path=/yourwebappcontext" | find "OK"
if errorlevel 1 (
    echo Restart failed
    exit /b 1
) else (
    echo Restart succeeded
)

When managing Java web applications in production environments, there are scenarios where daily restarts of specific webapps become necessary - whether for memory management, configuration updates, or other maintenance requirements. Tomcat 7 on Windows presents some unique challenges for this operation.

While Tomcat provides the Manager application with its web interface for reloading applications, it doesn't include built-in scheduling capabilities. The reload functionality in the Manager app essentially:

1. Undeploys the application
2. Redeploys it from the original location
3. Maintains all session data (if persistent)

The Tomcat Manager exposes a text-based API that's perfect for scripting. Here's the complete solution using Windows Task Scheduler and cURL:

@echo off
set TOMCAT_URL=http://localhost:8080
set APP_PATH=/yourwebapp
set MANAGER_USER=admin
set MANAGER_PASS=password

curl -u %MANAGER_USER%:%MANAGER_PASS% "%TOMCAT_URL%/manager/text/reload?path=%APP_PATH%"

For production environments, these security measures are essential:

  • Use HTTPS for manager access
  • Create dedicated manager roles with minimal privileges
  • Store credentials securely (consider Windows Credential Manager)
  • Restrict manager access to localhost or specific IPs

For environments where Manager access isn't available, JMX provides another option:

import javax.management.*;
import javax.management.remote.*;

JMXServiceURL url = new JMXServiceURL(
    "service:jmx:rmi:///jndi/rmi://localhost:8050/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

ObjectName manager = new ObjectName(
    "Catalina:type=Manager,context=/yourwebapp,host=localhost");
mbsc.invoke(manager, "stop", null, null);
mbsc.invoke(manager, "start", null, null);

To schedule daily execution:

  1. Create Basic Task in Task Scheduler
  2. Set trigger to "Daily" with your specific time
  3. Action: "Start a program" pointing to your batch file
  4. Set "Run whether user is logged on or not"
  5. Configure for your Windows version

Common issues and solutions:

Issue Solution
403 Forbidden Verify manager-gui role assigned in tomcat-users.xml
Application not restarting Check autoDeploy="true" in context.xml
Session data loss Configure persistent sessions
Port conflicts Verify no other service using port 8080