跪拜 Guibai
← All articles
Backend

Stop Using nohup java -jar: Three Production-Grade Ways to Daemonize Spring Boot

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

A Spring Boot process launched with `nohup &` will not restart after a crash or server reboot, logs will eventually exhaust the disk, and the service likely runs as root — a combination that turns a routine outage into a security incident. Adopting systemd or a container orchestrator eliminates these failure modes with a few lines of configuration and no new dependencies.

Summary

The `nohup java -jar &` one-liner is a development convenience that becomes a liability the moment a server reboots at 3 a.m. or a log file silently fills the disk. Production Spring Boot deployments need a process supervisor that can restart crashed services, rotate logs, and run under a dedicated non-root user.

Systemd, built into every modern Linux distribution, is the first choice: a service unit file defines the JVM arguments, the run-as user, restart policy, and log destination, all without installing extra software. For older CentOS 6 systems or cross-language environments, Supervisor provides a lightweight alternative with explicit control over process groups and log rotation. Teams already on containers get automatic restarts, rolling updates, and health checks from Kubernetes.

A production checklist rounds out the guidance — dedicated user accounts, explicit heap settings, externalized config, graceful shutdown via SIGTERM, log rotation, and firewall rules. The core message is that “it runs” is not enough; the service must survive reboots, crashes, and operator mistakes without manual intervention.

Takeaways
`nohup java -jar &` provides no auto-restart, no log rotation, and no permission control — it is not a process manager.
Systemd is the recommended choice on CentOS 7+ and Ubuntu 16.04+; it requires zero extra packages and handles restarts, logging, and boot startup.
Always create a dedicated non-login user (e.g., `useradd -r -s /bin/false app_user`) instead of running the service as root.
Set `SuccessExitStatus=143` in the systemd unit so Spring Boot’s `@PreDestroy` hooks execute on a normal SIGTERM shutdown.
Without root access, systemd user mode works if an admin enables linger with `loginctl enable-linger <username>`.
Supervisor fits legacy CentOS 6 systems or cross-language setups but is being deprecated on newer distributions; always use absolute paths for the Java command.
Docker plus Kubernetes provides automatic pod restarts, rolling zero-downtime updates, liveness and readiness probes, and horizontal scaling.
Externalize configuration with `--spring.config.location` rather than baking profiles into the JAR, and tune JVM flags explicitly instead of relying on defaults.
Conclusions

The gap between a development invocation and a production deployment is not about complexity — it is about failure modes that only surface at 3 a.m., and systemd closes that gap with a dozen lines of declarative config.

Supervisor’s requirement for absolute paths and explicit process-group killing reveals how many ‘mysterious’ production hangs trace back to orphaned Netty or thread-pool child processes that a simple kill missed.

Systemd user-mode with linger is an underused escape hatch in locked-down enterprise environments; it gives developers process supervision without sudo, provided an admin runs a single command once.

Concepts & terms
systemd
The default init system and service manager on modern Linux distributions (CentOS 7+, Ubuntu 16.04+). It starts, stops, and monitors services using declarative unit files, handling auto-restart, logging via journald, and boot-time ordering.
Supervisor
A lightweight, cross-language process control system that runs as a daemon and manages child processes. It restarts crashed processes, rotates logs, and operates without root after installation, but is being phased out on newer Linux distros in favor of systemd.
linger (loginctl)
A systemd-logind setting that keeps a user’s systemd user instance running after the user logs out. Enabling linger allows user-mode systemd services to survive logout, which is essential for running persistent services without root privileges.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗