不少时候,咱们须要将一些服务在Linux系统启动时即自动运行,免得每次都要去手动启动一遍,如Redis, MySQL, Nginx等。本文对CentOS与Ubuntu下开机自启动的配置方法进行整理,供参考查阅。nginx
rc.local是CentOS之前版本的方式,在CentOS7中仍然以兼容的形式存在,虽仍可用,但不推荐(推荐使用systemd service)。redis
一、编写须要开机自启动的脚本,并添加执行权限shell
[root@dev-server-1 ~]# vim test_rclocal.sh
#!/bin/bash
time=`date +%F_%T`
echo $time' from rc.local' >> /tmp/test.log
[root@dev-server-1 ~]# chmod +x test_rclocal.sh复制代码
做为测试,上述脚本打印一个时间到/tmp/test.log文件中ubuntu
二、在/etc/rc.d/rc.local配置文件中添加脚本运行命令(使用绝对路径)vim
[root@dev-server-1 ~]# vim /etc/rc.d/rc.local
#!/bin/bash
# ...注释部分
touch /var/lock/subsys/local
/root/test_rclocal.sh >/dev/null 2>/dev/null
复制代码
三、添加/etc/rc.d/rc.local文件的执行权限centos
在centos7中,/etc/rc.d/rc.local没有执行权限,须要手动受权bash
[root@dev-server-1 ~]# chmod +x /etc/rc.d/rc.local复制代码
以上三步,便可使/root/test_rclocal.sh >/dev/null 2>/dev/null
命令在服务器系统启动时自动运行。服务器
一、编写须要开机自启动的测试脚本,并添加执行权限微信
[root@dev-server-1 ~]# vim test_chkconfig.sh
#!/bin/bash
time=`date +%F_%T`
echo $time' from chkconfig' >> /tmp/test.log
[root@dev-server-1 ~]# chmod +x test_chkconfig.sh复制代码
二、在/etc/rc.d/init.d/目录下添加一个可执行脚本testchkconfig网络
[root@dev-server-1 ~]# vim /etc/rc.d/init.d/testchkconfig
#!/bin/bash
# chkconfig: 2345 90 10
# description: test chkconfig
/root/test_chkconfig.sh >/dev/null 2>/dev/null
[root@dev-server-1 ~]# chmod 755 /etc/rc.d/init.d/testchkconfig复制代码
上述testchkconfig脚本的头部必须遵循必定的格式 # chkconfig: 2345 90 10
, 其中2345指定服务在哪些执行等级中开启或关闭,90表示启动的优先级(0-100,越大优先级越低),10表示关闭的优先级。执行等级包括
三、加入开机启动服务列表
[root@dev-server-1 ~]# chkconfig --add testchkconfig
[root@dev-server-1 ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
testchkconfig 0:off 1:off 2:on 3:on 4:on 5:on 6:off复制代码
使用 chkconfig --list
可查看当前加入开机自启动的服务列表,但如Note部分所述,该命令只显示SysV服务,不包含原生的systemd服务,查看systemd服务可以使用systemctl list-unit-files
命令。
以上三步,便可使/root/test_chkconfig.sh >/dev/null 2>/dev/null
命令在服务器系统启动时自动运行。
chkconfig的其它命令参考
$chkconfig --list # 表示查看全部服务在各个运行级别下的状态。
$chkconfig testchkconfig on # 表示指定服务在全部的运行级别下都是开启状态。
$chkconfig testchkconfig off # 表示指定服务在全部的运行级别下都是关闭状态。
$chkconfig --level 5 testchkconfig on # 表示指定服务在运行级别5图形模式的状态下开机启动服务。
$chkconfig --level 5 testchkconfig off # 表示指定服务在运行级别5图形模式的状态下开机不启动服务。复制代码
CentOS7的systemd服务脚本存放在:/usr/lib/systemd/system(系统级)/usr/lib/systemd/user(用户级)下,以.service结尾。这里以nginx为例
一、在/usr/lib/systemd/system目录下建立nginx.service文件
[devuser@test-server-1 ~]$ sudo vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server
Documentation=http://nginx.org/en/docs/
# 依赖服务,仅当依赖的服务启动以后再启动自定义的服务
After=network.target remote-fs.target nss-lookup.target
[Service]
# 启动类型,包括simple、forking、oneshot、notify、dbus
Type=forking
# pid文件路径
PIDFile=/var/run/nginx.pid
# 启动前执行的操做
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
# 启动命令
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 重载命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
# 中止命令
ExecStop=/usr/local/nginx/sbin/nginx -s stop
# 是否给服务分配独立的临时空间
PrivateTmp=true
[Install]
# 服务安装的用户模式,通常使用multi-user便可
WantedBy=multi-user.target复制代码
其中Service部分的Type包括以下几种类型:
二、 开启开机自启动
[devuser@test-server-1 ~]$ sudo systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.复制代码
以上两步,就将nginx服务配置成了在操做系统启动时自动启动。
其它命令参考
$sudo systemctl start nginx.service # 启动
$sudo systemctl restart nginx.service # 重启
$sudo systemctl reload nginx.service # 重载
$sudo systemctl stop nginx.service # 中止
$sudo systemctl status nginx.service # 查看服务状态
$sudo systemctl cat nginx.service # 查看服务配置
$systemctl list-unit-files |grep nginx # 查看服务enabled状态
$sudo systemctl disable nginx.service # 关闭开机自启动
$sudo journalctl -f -u nginx.service # 查看日志
$sudo systemctl daemon-reload # 配置修改后,从新加载复制代码
根据以上配置,经过start启动nginx服务时,报
PID file /var/run/nginx.pid not readable (yet?) after start.
的错误,启动失败,日志以下
[devuser@test-server-1 ~]$ sudo journalctl -f -u nginx.service
-- Logs begin at Wed 2020-03-25 09:14:55 CST. --
Mar 25 11:02:27 test-server-1 nginx[14144]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Mar 25 11:02:27 test-server-1 systemd[1]: PID file /run/nginx.pid not readable (yet?) after start.
Mar 25 11:04:29 test-server-1 systemd[1]: nginx.service start operation timed out. Terminating.
Mar 25 11:04:29 test-server-1 systemd[1]: Failed to start nginx.
Mar 25 11:04:29 test-server-1 systemd[1]: Unit nginx.service entered failed state.
Mar 25 11:04:29 test-server-1 systemd[1]: nginx.service failed.复制代码
从字面看是PID文件不可读,查看/var/run/nginx.pid,该文件也确实不存在,查看nginx.conf配置文件,发现是pid /var/run/nginx.pid;
这行配置被注释掉了, 若是不指定pid文件位置,nginx默认会把pid文件保存在logs目录中。因此出现systemd启动服务时找不到pid文件而报错,将nginx.conf中的pid配置注释去掉,重启nginx.service便可。
在Ubuntu18.04中,主要也是以systemd服务来实现开机自启动,systemd默认读取/etc/systemd/system/下的配置文件,该目录下的一些文件会连接到/lib/systemd/system/下的文件。
所以能够在/etc/systemd/system/目录下面建立一个自启动服务配置,之内网穿透服务frp客户端为例,如
[Unit]
Description=frpc
After=network.target
Wants=network.target
[Service]
TimeoutStartSec=30
ExecStart=/home/devuser/apps/frp/frpc -c /home/devuser/apps/frp/frpc.ini
ExecStop=/bin/kill $MAINPID
Restart=1
[Install]
WantedBy=multi-user.target复制代码
各配置项与CentOS相似。而后将服务器加到自启动列表中并启动服务
$sudo systemctl enable frpc
$sudo systemctl start frpc复制代码
其它更多systemctl命令与CentOS相似。
也可使用/lib/systemd/system/rc-local.service来执行一些开机须要执行的脚本,该文件内容为
# SPDX-License-Identifier: LGPL-2.1+
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no复制代码
从Description看它是为了兼容以前版本的/etc/rc.local的,该服务启动命名就是/etc/rc.local start
,将该文件连接到/etc/systemd/system下
$ sudo ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service 复制代码
建立/etc/rc.local文件,并赋予可执行权限
$ vim /etc/rc.local
#!/bin/bash
echo "test rc " > /var/test.log
$ sudo chmod +x /etc/rc.local复制代码
做者:空山新雨,一枚仍在学习路上的IT老兵 近期做者写了几十篇技术博客,内容包括Java、Spring Boot、Spring Cloud、Docker,技术管理心得等
欢迎关注做者微信公众号:空山新雨的技术空间,一块儿学习成长