You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Play Framework因异常崩溃时自动重启?

Great question! Even though Play Framework is built to be rock-solid and avoid crashes by design, we all know unexpected issues—like unhandled exceptions, resource leaks, or external service outages—can take an app down. Here are the most practical, battle-tested ways to set up automatic restarts for your Play app:

Automatic Restart Solutions for Play Framework Apps

1. Systemd (Linux Production Standard)

Systemd is the go-to init system on nearly all modern Linux distros, and it’s perfect for ensuring your Play app stays running.

  • First, create a systemd service file (e.g., /etc/systemd/system/play-app.service):
    [Unit]
    Description=My Play Framework Application
    After=network.target
    
    [Service]
    User=your-app-user
    Group=your-app-group
    WorkingDirectory=/path/to/your/app
    ExecStart=/usr/bin/java -jar your-play-app.jar
    Restart=always
    RestartSec=5
    Environment="PLAY_CONF_FILE=/path/to/application.conf"
    
    [Install]
    WantedBy=multi-user.target
    
  • Key details to note:
    • Restart=always: Tells systemd to restart the service no matter why it exited—including crashes.
    • RestartSec=5: Waits 5 seconds before restarting to avoid thrashing if the app crashes immediately on startup.
  • Then set up and start the service:
    sudo systemctl daemon-reload
    sudo systemctl enable play-app.service
    sudo systemctl start play-app.service
    
  • Check status or live logs anytime with:
    sudo systemctl status play-app.service
    journalctl -u play-app.service -f
    

2. Supervisor (Lightweight Cross-Platform Option)

Supervisor is a simple process control system that works great for Play apps, especially if you’re on a Linux distro without systemd or prefer a more flexible tool.

  • Install Supervisor first (e.g., on Debian/Ubuntu: sudo apt-get install supervisor).
  • Create a config file in /etc/supervisor/conf.d/play-app.conf:
    [program:play-app]
    command=/usr/bin/java -jar /path/to/your-app/your-play-app.jar
    directory=/path/to/your-app
    user=your-app-user
    autorestart=true
    startretries=3
    redirect_stderr=true
    stdout_logfile=/var/log/play-app.log
    
  • Critical config options:
    • autorestart=true: Automatically restarts the process if it crashes.
    • startretries=3: Gives up after 3 consecutive failed starts (adjust this number based on your needs).
  • Update Supervisor and launch your app:
    sudo supervisorctl reread
    sudo supervisorctl update
    sudo supervisorctl start play-app
    
  • Check the app’s status with sudo supervisorctl status play-app.

3. Play’s Built-in Dev Mode (For Local Development Only)

If you’re working locally, Play’s dev mode already handles automatic restarts—both for code changes and unexpected crashes. Just run:

sbt run

Or if you’re using Activator:

activator run

Heads up: This mode is not meant for production. Stick to the process managers above for live environments.

4. Docker Restart Policies (Modern Containerized Deployments)

If you’re deploying your Play app with Docker, you can use Docker’s built-in restart policies to keep it alive.

  • When running your container directly, use the --restart flag:
    docker run -d --restart always -p 9000:9000 your-play-app-image
    
  • Available --restart options:
    • always: Restarts the container no matter why it exits.
    • on-failure:3: Restarts only if the container exits with a non-zero code, up to 3 times.
  • For Docker Compose, add the restart policy to your service definition:
    services:
      play-app:
        image: your-play-app-image
        ports:
          - "9000:9000"
        restart: always
    

Quick Pro Tips

  • Graceful Shutdown: Make sure your app handles shutdown properly! Use Play’s ApplicationLifecycle hooks to clean up connections or save state—this prevents data corruption when restarts happen.
  • Don’t Ignore Crashes: Automatic restarts are a safety net, not a fix. Always monitor your logs to track down why crashes are occurring in the first place.

内容的提问来源于stack exchange,提问作者vaj oja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:39:33