我不建议在/etc/rc.local
. 这是旧的 unix 时代的遗物。有些 Linux 不再支持rc.local
.
但是,它可能会正确启动您的应用程序/服务,但它永远不会优雅地关闭您的进程。
最好使用系统自己的 init 脚本机制(Systemd,Upstart,...)。我会写一个像这样的 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)
归档时间: |
|
查看次数: |
19369 次 |
最近记录: |