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

如何让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.

Most current Linux distributions (like Ubuntu 16.04+, CentOS 7+, Debian 9+) use systemd as their init system. This is the cleanest approach:

  1. Create a systemd service file
    Create a file at /etc/systemd/system/my-app-stack.service with 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) mode
    
  2. Enable 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.service
    

    To 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:

  1. Create an init script
    Create a file at /etc/init.d/my-app-startup with 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 0
    
  2. Make 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 on
    

    Test it with:

    sudo service my-app-startup start
    

Key Notes to Remember

  • Why su - user instead of just su user? The - flag triggers a login shell, which automatically loads the user’s .profile (and other login-specific config files). This means all your JAVA_HOME exports, sourced configs, and umask settings will be applied exactly as they are when you manually log in with sudo 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-base needs Tomcat to be running), you might want to add small sleep commands between them, or add checks to verify the previous app started successfully.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:41:36