Ver Fonte

linux服务自启动

kindring há 3 anos atrás
pai
commit
dd2f90af58
1 ficheiros alterados com 59 adições e 15 exclusões
  1. 59 15
      linux/开机自启相关.md

+ 59 - 15
linux/开机自启相关.md

@@ -1,15 +1,59 @@
-# linux开机自启动相关
-## 如何查看开机自启动
-> 查看服务等
-+ 查看服务的方式,如下命令
-  ```
-    systemctl list-unit-files
-  ```
-  + 加上过滤
-    ```
-        systemctl list-unit-files | grep enable
-    ```
-## 设置开机自启项
-1. 创建服务
-2. 启动服务
-3. 设置服务自启
+# 使用 `systemctl` 配置服务.并且使其自启动
+## 环境介绍
+> [!note]
+> ubuntu 21.04
+### 定义服务
+在 `/lib/systemd/system/` 目录下创建文件 `serverName.service`
+> [!warn]
+> serverName 替换为自己需要的服务名称,可以直接执行下面的代码
+
+修改文件,使用`vim`来进行编辑, 在shell
+```shell
+vim /lib/systemd/system/serverName.service
+```
+通过`vim`打开`serverName.service`后继续输入下方代码,其中的 `server` 替换成执行任务
+```shell
+[Unit]
+Description=fraps service
+After=network.target network-online.target syslog.target
+Wants=network.target network-online.target
+[Service]
+Type=simple
+#要启动的命令(此处根据实际情况填写)
+ExecStart= server
+[Install]
+WantedBy=multi-user.target
+```
+### 启动服务
+```shell
+sudo systemctl start serverName
+```
+### 服务加入到启动项
+```shell
+sudo systemctl enable serverName
+```
+### 常用命令
+> [!tip]
+> 常用命令基本上分为 start restart stop 
+ <!-- tabs:start -->
+#### **启动服务**
+```shell
+sudo systemctl start serverName
+```
+#### **重启服务**
+```shell
+sudo systemctl restart serverName
+```
+#### **关闭服务**
+```shell
+sudo systemctl stop serverName
+```
+#### **启用服务**
+```shell
+sudo systemctl enable serverName
+```
+#### **停用服务**
+```shell
+sudo systemctl disable serverName
+```
+ <!-- tabs:end -->