miho

一. 介绍

  Linux中很多手动安装的程序,开机是不会自启,但是我们又有这个需求!例如: NginxMysqlRedis等等服务。如果开机不自启就意味着,服务器宕机或者重启了,不去手动启动会导致系统无法使用!许多程序的自启动,给我们带来了很多方便,这是不争的事实,但不是每个自启动的程序对我们都有用。所以根据需要的情况酌情处理!

二. 配置

2.1 进入系统Service文件存放目录

1
$ # cd /lib/systemd/system/

2.2 创建Service配置文件

此处以nginx-1.13.0.service服务脚本为例,服务依赖以及启动命令自己百度,每个服务都不是完全一样!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ # vim nginx-1.13.0.service
######################## 配置如下 ########################
[Unit]
Description=nginx-1.13.0
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking

# 设置环境变量
Environment=JAVA_HOME=/usr/local/java/jdk1.8.0_131

ExecStart=/usr/local/nginx-1.13.0/sbin/nginx -c /usr/local/nginx-1.13.0/conf/nginx.conf
ExecStop=/usr/local/nginx-1.13.0/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

2.3 设置脚本开机自启

1
$ # systemctl enable nginx-1.13.0.service

2.4 重启网络服务

1
# service network restart

三. 常用命令

以下是基本的操作命令。

3.1 命令如下

1
2
3
4
5
6
7
8
9
10
重新加载service文件:systemctl daemon-reload
启动一个服务:systemctl start nginx-1.13.0.service
关闭一个服务:systemctl stop nginx-1.13.0.service
重启一个服务:systemctl restart nginx-1.13.0.service
显示一个服务的状态:systemctl status nginx-1.13.0.service
在开机时启用一个服务:systemctl enable nginx-1.13.0.service
在开机时禁用一个服务:systemctl disable nginx-1.13.0.service
查看服务是否开机启动:systemctl is-enabled nginx-1.13.0.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed

最后更新: 2021年04月01日 11:03

原始链接: https://www.lmaye.com/2017/12/23/20180809103413/

× 多少都行~
打赏二维码