Alpine 使用OpenRC设置程序开机自启动

需求

使用 docker start xxx docker restart xxx 启动基于Alpine 的容器后,自动运行自定义的服务。

分析

Alpine使用OpenRC管理服务启动、停止、查看状态及加入启动项。Alpine官方文档中Working with OpenRC章节介绍如下:

OpenRC is the init system used in alpine. The init system manages the services, startup and shutdown of your computer.

OpenRC 是 alpine 中使用的初始化系统。init 系统管理计算机的服务、启动和关闭。

OpenRC

Alpine 默认没有安装 openrc ,可以通过以下命令安装

apk add openrc

安装成功后 /etc/ 下将增加一个 local.d/ 目录

注意:/etc/local.d/目录是安装了openrc 后才有的

/etc/local.d/ 目录就是用于做自启程序的,方法见其下 README 文件,内容如下:

This directory should contain programs or scripts which are to be run
when the local service is started or stopped.

If a file in this directory is executable and it has a .start extension,
it will be run when the local service is started. If a file is
executable and it has a .stop extension, it will be run when the local
service is stopped.

All files are processed in lexical order.

Keep in mind that files in this directory are processed sequentially,
and the local service is not considered started or stopped until
everything is processed, so if you have a process which takes a long
time to run, it can delay your boot or shutdown processing.

除了 /etc/local.d/ 文件夹外,openrc 提供了openrc openrc-init openrc-run openrc-shutdownrc-service rc-sstat rc-statusrc-update 管理服务的命令

程序自启动示例

根据 /etc/local.d/README 介绍, 如果想让程序在启动后执行,可创建一个以.start 结尾的脚本;如果想让程序在容器停止运行时运行,可以创建一个以.stop 结尾的脚本。

以下展示自启动。

需求
让容器启动后运行 live555MediaServer 程序。

步骤
1、在 /etc/local.d/ 下创建脚本,live555MediaServer.start ,内容如下:

cd /data/live/mediaServer/
./live555MediaServer &

**注意:**还有2步需要做才能开机自启。
2、 启用local服务的开机自启 rc-update add local
3、 授予.start脚本的执行权限 chmod +x /etc/local.d/test.start

只需要执行过1次 rc-update add local,以后都直接在/etc/local.d/目录,添加.start脚本并授予执行权限,即可让该脚本开机自启

总结

经反复测试,效果不佳。欢迎各方大佬提出更改意见及方案,谢谢。

另一种开机自启方案,使用 systemd 管理相关服务。如在 ubuntu 操作系统下,可以无需安装即可使用 systemd 来完成自启。

参考:
【 Ubuntu】systemd服务自启
【Ubuntu】systemd 及其工具
【ubuntu】systemd 管理系统组件和服务之间的依赖关系

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值