searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

Ubuntu如何使用systemd设置开机自启功能

2024-08-21 09:43:08
152
0

1.介绍

systemd 是一套基本构建模块,它作为一个系统和服务管理器,可以用来启动和管理 Linux 操作系统中的各种服务和进程。当进程在 Linux 系统上崩溃时,也可以通过配置 systemd 设置自动启动服务功能,以减少停机时间并提高服务可用性。

2.检查安装

可以使用下述命令查询系统中是否安装了 systemd 工具:

systemd --version

如果系统返回了类似如下的信息,表面该系统是由 systemd 来管理软件的:

systemd 245 (245.4-4ubuntu3.15)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid

3.设置配置文件

假设需要运行的脚本文件为 /home/autoStart.sh,为了使用 systemd,我们需要在目录 /etc/systemd/system 中添加后缀为 .service 的配置文件(如:autostart.service),示例如下:

[Unit]
Description=auto start tool
Documentation=
After=network.target
Wants=
Requires=

[Service]
Type=forking
ExecStart=/home/autoStart.sh
ExecStop=
ExecReload=/home/autoStart.sh
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

4.注意事项

(1)使用如下命令测试配置文件 autostart.service 是否能够正常运行:

sudo systemctl start autostart.service

(2)如果能够正常运行,设置开机自启:

sudo systemctl enable autostart.service

(3)如果修改了配置文件 autostart.service,需要使用如下命令重新加载配置文件然后重新启动,否则修改不会生效:

sudo systemctl daemon-reload
sudo systemctl restart autostart.service

 

0条评论
0 / 1000
zhoux27
4文章数
0粉丝数
zhoux27
4 文章 | 0 粉丝
原创

Ubuntu如何使用systemd设置开机自启功能

2024-08-21 09:43:08
152
0

1.介绍

systemd 是一套基本构建模块,它作为一个系统和服务管理器,可以用来启动和管理 Linux 操作系统中的各种服务和进程。当进程在 Linux 系统上崩溃时,也可以通过配置 systemd 设置自动启动服务功能,以减少停机时间并提高服务可用性。

2.检查安装

可以使用下述命令查询系统中是否安装了 systemd 工具:

systemd --version

如果系统返回了类似如下的信息,表面该系统是由 systemd 来管理软件的:

systemd 245 (245.4-4ubuntu3.15)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid

3.设置配置文件

假设需要运行的脚本文件为 /home/autoStart.sh,为了使用 systemd,我们需要在目录 /etc/systemd/system 中添加后缀为 .service 的配置文件(如:autostart.service),示例如下:

[Unit]
Description=auto start tool
Documentation=
After=network.target
Wants=
Requires=

[Service]
Type=forking
ExecStart=/home/autoStart.sh
ExecStop=
ExecReload=/home/autoStart.sh
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

4.注意事项

(1)使用如下命令测试配置文件 autostart.service 是否能够正常运行:

sudo systemctl start autostart.service

(2)如果能够正常运行,设置开机自启:

sudo systemctl enable autostart.service

(3)如果修改了配置文件 autostart.service,需要使用如下命令重新加载配置文件然后重新启动,否则修改不会生效:

sudo systemctl daemon-reload
sudo systemctl restart autostart.service

 

文章来自个人专栏
文章 | 订阅
0条评论
0 / 1000
请输入你的评论
0
0