DevOps—Ubuntu20启动时执行命令

概述

在系统启动时想要执行一些语句或脚本,可以使用Systemd的方式进行处理。这种方式也可以添加一些判断条件,例如在网络连接成功后执行。

方法

1、创建systemd的service文件

vim /lib/systemd/system/myiscsi.service

内容如下:

[Unit]
Description=Job that runs your user script
Wants=network-online.target

[Service]
ExecStart=需要执行的命令
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

注意:要执行的语句或脚本需要在网络连接成功能才能执行,需要添加Wants的字段,如果不需要等待网络,可以将该行删除即可。

2、创建service

systemctl enable myiscsi.service
systemctl start myiscsi.service
systemctl status myiscsi.service

注意,service的名称如果不符合要求(可能会有某个服务的名称冲突)可能会导致服务启动不成功,请使用新名称后重试即可。

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注