Alpine Linux 实现开机自启脚本

安装 rc-service 服务

Alpine使用的是OpenRCinit系统,而rc-service是openrc的一部分,所以安装openrc即可,使用如下命令安装:

apk add openrc --no-cache

管理服务

  • 列出所有可用服务
    rc-service --list
  • 启动/停止/重启 已有服务
    rc-service 服务名 start/stop/restart

    OR

    /etc/init.d/服务名 start/stop/restart

设置开机自启动

rc-update add {service-name}

———————————-分割线———————————-

设置socat开机自启

Alpine Linux 的 开机自启目录在/etc/local.d下,这个目录用于放置我们需要在本地服务启动或停止后执行的脚本。

先上目录下的说明文档:

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.

简单翻译了下:

此目录应包含在启动或停止本地服务时要运行的程序或脚本。

如果此目录中的文件是可执行文件,并且扩展名为.start,则它将在启动本地服务时运行。 如果文件是可执行文件,并且扩展名为.stop,则它将在本地服务停止时运行。

所有文件均按词法顺序处理。

请记住,此目录中的文件是按顺序处理的,在处理完所有内容之前,不会将本地服务视为已启动或停止,因此,如果您的进程运行时间很长,则可能会延迟启动或关闭进程。

我们现在需要开机自启ss服务,由于Alpine Linux自带了nohup后台守护,我们就直接使用nohup设置开机自启。

先编辑下ss.start文件,注意,.start后缀需要固定,前面名称随意:

vi socat.start //编辑文件
nohup socat TCP4-LISTEN:12345,reuseaddr,fork TCP4:1.1.1.1:18888  //填充数据

然后保存退出。

赋予脚本可执行权限:

chmod +x socat.start

设置 local 服务开机启动:

rc-update add local

这样当系统开机时就能执行socat.start开达到开机自动的目的。

上一篇
下一篇