使应用程序自动启动

use*_*931 3 linux centos

这里我有一个应用程序部署到 linux,我希望该应用程序在 linux 启动时自动启动。我正在使用类似'sudo ./start'启动应用程序的命令。我怎样才能做到这一点?

操作系统:CentOS 6

cha*_*aos 8

我不建议在/etc/rc.local. 这是旧的 unix 时代的遗物。有些 Linux 不再支持rc.local.

但是,它可能会正确启动您的应用程序/服务,但它永远不会优雅地关闭您的进程。

最好使用系统自己的 init 脚本机制(SystemdUpstart,...)。我会写一个像这样的 rc 脚本(你的系统上可能有一个骨架/模板/etc/init.d/skeleton):

#!/bin/bash
. /etc/init.d/functions

start() {
        echo -n "Starting <servicename>: "
        #/path/to/the/executable/of/your/application
}

stop() {
        echo -n "Shutting down <servicename>: "
        #command_to_gracefully_end_the_application
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
    #command_to_report_the_status
    ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: <servicename> {start|stop|restart}"
        exit 1
        ;;
esac
exit $?
Run Code Online (Sandbox Code Playgroud)

将您的脚本放在 /etc/init.d/ 中,使其可执行并将其添加到系统运行级别3、4 和 5:

chkconfig --level 345 <servicename> on
Run Code Online (Sandbox Code Playgroud)

您也可以手动启动和停止它:

service <servicename> start
service <servicename> stop
Run Code Online (Sandbox Code Playgroud)