方式一:Linux开机自启动脚本

树莓派OPENFANS(Debian-Pi-Aarch64) :

系统支持自定义任务自启动脚本,可以在系统启动前预先配置。

编辑脚本文件 /boot/rc-local,在 “exit 0” 前加入自定义的脚本内容。

例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "IP address is %s\n" "$_IP"
fi

echo "rc-local bash echo test."

#下面为添加的java项目启动命令
nohup /xxx/jdk1.8.0_291/bin/java -jar /xxx/xxx/xxx.jar --server.port=8080 -Dfile.encoding=UTF-8 &

exit 0

方式二:Systemd开机启动

进入到对应的Systemd目录下新建配置文件:

CentOS 7 中存放Systemd 配置文件路径:/usr/lib/systemd/system

树莓派OPENFANS(Debian-Pi-Aarch64) 中存放Systemd 配置文件路径:/lib/systemd/system

例1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[Unit]
#配置描述
Description=nginx - high performance web server
#文档地址
Documentation=http://nginx.org/en/docs/
#前置任务
After=network.target

[Service]
#启动类型
Type=forking
#启动命令
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/imxt.conf
#重启命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#停止命令
ExecStop=/usr/local/nginx/sbin/nginx -s stop
#是否使用私有tmp目录
PrivateTmp=true

[Install]
#服务组
WantedBy=multi-user.target

例2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[Unit]
#配置描述
Description=frp service
#文档地址
Documentation=https://gofrp.org/docs/
#前置任务
After=network.target syslog.target
#依赖关系
Wants=network.target

[Service]
#启动类型
Type=simple
#启动服务之前执行的命令
ExecStartPre=/bin/sleep 10
#启动命令
ExecStart=

[Install]
#服务组
WantedBy=multi-user.target

常用的配置操作命令:

启动服务:systemctl start frp.service

停止服务:systemctl stop frp.service

重启服务:systemctl restart frp.service

查看服务状态:systemctl status frp.service

重新加载配置:systemctl reload frp.service

重新加载所有配置:systemctl daemon-reload

列出所有配置文件:systemctl list-unit-files

列出指定类型的配置文件:systemctl list-unit-files --type=service

开启开机自启:systemctl enable frp.service

关闭开机自启:systemctl disable frp.service

一共有四种状态类型:

  • enabled:已建立启动链接
  • disabled:没建立启动链接
  • static:该配置文件没有 [Install] 部分(无法执行),只能作为其他配置文件的依赖
  • masked:该配置文件被禁止建立启动链接

相关资料查阅:

Linux 守护进程的启动方法 - 阮一峰的网络日志

Systemd 入门教程:命令篇 - 阮一峰的网络日志

Systemd 入门教程:实战篇 - 阮一峰的网络日志