如何监控jar文件变更并自动重启?含app.jar替换重启场景
Hey there! Let's figure out how to get your Java app to auto-restart whenever you replace or update app.jar. I've got a couple of solid methods for you, depending on what your server setup looks like:
Method 1: Shell Script + inotifywait (Lightweight & Flexible)
This is a straightforward approach that uses file system event monitoring to trigger restarts. First, you'll need inotify-tools installed—it lets you track changes to files in real-time.
Install it on Debian/Ubuntu:
sudo apt update && sudo apt install inotify-tools
Or on RHEL/CentOS:
sudo yum install inotify-tools
Next, create a monitoring script (name it app_monitor.sh):
#!/bin/bash APP_JAR="/path/to/your/app.jar" PID_FILE="/tmp/app.pid" # Start the application start_app() { echo "Booting up app.jar..." nohup java -jar "$APP_JAR" > app.log 2>&1 & echo $! > "$PID_FILE" echo "App started with PID $(cat $PID_FILE)" } # Stop the application gracefully stop_app() { if [ -f "$PID_FILE" ]; then PID=$(cat "$PID_FILE") echo "Shutting down app with PID $PID..." kill $PID rm "$PID_FILE" sleep 2 # Give it a moment to exit completely fi } # Launch the app initially start_app # Monitor for changes to app.jar echo "Watching $APP_JAR for updates..." while true; do # Trigger on file modifications, saves, or moves (like replacing the jar) inotifywait -e modify,close_write,moved_to "$APP_JAR" echo "Detected change in app.jar—restarting..." stop_app start_app done
Make the script executable:
chmod +x app_monitor.sh
Run it in the background so it keeps working even after you log out:
nohup ./app_monitor.sh > monitor.log 2>&1 &
Pro tip: When replacing app.jar, use an atomic move to avoid partial file writes triggering false restarts:
# Upload your new jar as app.jar.tmp first, then: mv app.jar.tmp app.jar
Method 2: Systemd Service (For Modern Linux Distros)
If your server uses systemd (most distros like Ubuntu 16.04+, CentOS 7+), this is the cleanest, most integrated approach. Systemd handles process management and file monitoring natively.
Create a systemd service file at /etc/systemd/system/app.service:
[Unit] Description=My Java Application After=network.target [Service] Type=simple ExecStart=/usr/bin/java -jar /path/to/your/app.jar Restart=always # Auto-restart when app.jar changes PathChanged=/path/to/your/app.jar User=your_username # Replace with the user that should run the app WorkingDirectory=/path/to/your/app_dir # Optional: set the app's working directory [Install] WantedBy=multi-user.target
Reload systemd to recognize the new service:
sudo systemctl daemon-reload
Enable the service to start on boot:
sudo systemctl enable app.service
Start the service:
sudo systemctl start app.service
Now whenever you update or replace app.jar, systemd will automatically restart the service. You can check its status anytime with:
sudo systemctl status app.service
Method 3: Supervisor (Advanced Process Management)
If you're already using Supervisor to manage server processes, you can pair it with inotifywait to trigger restarts on file changes.
First, install Supervisor (if not already set up):
# Debian/Ubuntu sudo apt install supervisor # RHEL/CentOS sudo yum install supervisor
Create a Supervisor config for your app at /etc/supervisor/conf.d/app.conf:
[program:my_app] command=/usr/bin/java -jar /path/to/your/app.jar directory=/path/to/your/app_dir user=your_username autostart=true autorestart=true stdout_logfile=/var/log/app_stdout.log stderr_logfile=/var/log/app_stderr.log
Reload Supervisor to apply the config:
sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start my_app
Modify the monitoring script from Method 1 to use Supervisor's restart command instead of manual PID management:
#!/bin/bash APP_JAR="/path/to/your/app.jar" # Restart via Supervisor restart_app() { echo "Restarting app through Supervisor..." sudo supervisorctl restart my_app } echo "Watching $APP_JAR for updates..." while true; do inotifywait -e modify,close_write,moved_to "$APP_JAR" echo "Detected change in app.jar—restarting..." restart_app done
Make it executable and run it in the background just like before.
内容的提问来源于stack exchange,提问作者lessisawesome

