开机自启动设置 开机自启动( 三 )


— 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的开机自启动配置
在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复制代码<完>
【开机自启动设置 开机自启动】作者:半路雨歌链接:https://juejin.cn/post/6844904104515338248

推荐阅读