跪拜 Guibai
← All articles
Backend

Stop Using nohup java -jar: How to Keep a Spring Boot App Alive in Production

By 神奇小汤圆 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A Spring Boot process started with `nohup` will not survive a server reboot, a JVM crash, or a full disk from unrotated logs. The fix is a few lines in a systemd unit file — no extra software, no cost — and it turns a fragile dev habit into a service that restarts itself and logs properly.

Summary

Running a Spring Boot jar with `nohup java -jar &` leaves a production service without automatic restarts, log rotation, or startup-on-boot. When the server reboots, the process stays dead until someone manually revives it, and hunting for PIDs to restart or stop the app risks collateral damage. The core problem is that `nohup` only ignores the hangup signal; it was never designed to supervise a long-running daemon.

systemd, built into every modern Linux distribution, solves this with a simple unit file that defines the Java command, the runtime user, restart policy, and log output. For environments without root access, systemd's user mode works after an administrator enables lingering. Supervisor remains a fallback for older CentOS 6 systems or cross-language workloads, though it is being phased out on newer distributions. Docker plus Kubernetes shifts the responsibility to the orchestrator, which handles health checks, rolling updates, and horizontal scaling.

A production checklist rounds out the advice: never run as root, pin JVM heap and GC settings, externalize configuration, enable graceful shutdown with SIGTERM, rotate logs, and match the Java version to the Spring Boot release. The through-line is that a service that "just runs" in dev needs explicit supervision to survive a year of unattended operation.

Takeaways
`nohup java -jar &` ignores the hangup signal but provides no supervision, restart logic, or log management.
systemd is the default service manager on CentOS 7+ and Ubuntu 16.04+ and requires no additional packages.
A systemd unit file sets the runtime user, JVM options, restart policy, and log output in one place.
Setting `SuccessExitStatus=143` tells systemd that a SIGTERM shutdown is normal, preserving Spring Boot's graceful shutdown hooks.
Without root, systemd user mode works after an admin runs `loginctl enable-linger` once for the account.
Supervisor needs absolute paths for the Java binary because it does not inherit the shell's `$PATH`.
Supervisor's `stopasgroup` and `killasgroup` flags prevent orphaned Netty or thread-pool child processes.
Kubernetes provides automatic restarts, rolling updates, liveness/readiness probes, and horizontal scaling out of the box.
Production deployments must use a dedicated non-root user, externalized config, tuned JVM heap and GC, and log rotation.
Conclusions

The gap between 'it runs' and 'it stays running' is entirely about process supervision, not application code, yet it's one of the most common causes of production incidents in small teams.

systemd's user-mode linger feature is under-documented but critical in locked-down enterprise environments where developers lack sudo; it lets a user-level service survive logout.

Supervisor's decline on CentOS 7+ is a reminder that even widely adopted tools become legacy when the OS absorbs their function natively.

Concepts & terms
systemd
The system and service manager for modern Linux distributions that handles boot sequencing, process supervision, logging, and resource control through unit files.
SuccessExitStatus=143
A systemd directive that treats exit code 143 (SIGTERM) as a clean shutdown, preventing systemd from marking a service as failed when it is stopped gracefully.
linger (loginctl enable-linger)
A systemd-logind setting that allows a user's systemd user instance to start at boot and keep running after the user logs out, enabling user-level services to persist.
Supervisor
A cross-language process control system for UNIX-like operating systems that starts, stops, and monitors processes, often used as a lightweight alternative to systemd on older distributions.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗