如何让Linux服务器开机自动启动指定应用(含环境变量配置)?
Absolutely! You can absolutely automate this workflow so your applications launch automatically every time the server boots up—and handling the .profile configurations is totally straightforward. Let’s walk through the best ways to set this up, covering both modern and traditional Linux init systems.
Method 1: Systemd Service (Recommended for Modern Distros)
Most current Linux distributions (like Ubuntu 16.04+, CentOS 7+, Debian 9+) use systemd as their init system. This is the cleanest approach:
Create a systemd service file
Create a file at/etc/systemd/system/my-app-stack.servicewith the following content:[Unit] Description=Automatically start my application stack on boot After=network.target # Ensure network is up before starting apps [Service] Type=oneshot User=root # Use `su - user` to trigger a login shell, which automatically loads the user's .profile ExecStart=/bin/su - user -c "/opt/tomcat/bin/startup.sh && /opt/config/start && /opt/config/start-base prod" RemainAfterExit=yes # Tells systemd the service stays "active" after commands run [Install] WantedBy=multi-user.target # Start when server reaches multi-user (non-graphical) modeEnable and test the service
Run these commands to register the service and set it to start on boot:sudo systemctl daemon-reload sudo systemctl enable my-app-stack.serviceTo test it without rebooting:
sudo systemctl start my-app-stack.service
Method 2: SysV Init Script (For Older Distros)
If you’re running an older Linux distro that uses SysV init (like CentOS 6, Ubuntu 14.04), use this method:
Create an init script
Create a file at/etc/init.d/my-app-startupwith this content:#!/bin/bash # chkconfig: 345 99 10 # description: Starts my application stack on system boot case "$1" in start) # `su - user` loads the user's .profile automatically via a login shell su - user -c "/opt/tomcat/bin/startup.sh && /opt/config/start && /opt/config/start-base prod" ;; stop) # Optional: Add stop commands if you need them (e.g., shutdown Tomcat) # su - user -c "/opt/tomcat/bin/shutdown.sh && /opt/config/stop" ;; restart) $0 stop sleep 5 $0 start ;; *) echo "Usage: $0 {start|stop|restart}" exit 1 ;; esac exit 0Make the script executable and enable it
sudo chmod +x /etc/init.d/my-app-startup sudo chkconfig --add my-app-startup sudo chkconfig my-app-startup onTest it with:
sudo service my-app-startup start
Key Notes to Remember
- Why
su - userinstead of justsu user? The-flag triggers a login shell, which automatically loads the user’s.profile(and other login-specific config files). This means all yourJAVA_HOMEexports, sourced configs, andumasksettings will be applied exactly as they are when you manually log in withsudo su - user. - Test first! Before relying on the boot automation, run the command
su - user -c "/opt/tomcat/bin/startup.sh && /opt/config/start && /opt/config/start-base prod"manually to confirm it launches your apps correctly. Fix any permission or path issues first. - Order matters: If some apps depend on others (e.g., your
start-baseneeds Tomcat to be running), you might want to add smallsleepcommands between them, or add checks to verify the previous app started successfully.
内容的提问来源于stack exchange,提问作者fabje

