参考网址:

(91条消息) Ubuntu18.04 开机自启动脚本_cxh的博客-CSDN博客_ubuntu18.04开机自启动脚本

(91条消息) linux如何后台运行服务_六指黑侠i的博客-CSDN博客_linux 后台运行

自启动

1
2
# rc-local.service生成
sudo nano /etc/systemd/system/rc-local.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local #脚本文件位置

[Service]
Type=forking
ExecStart=/etc/rc.local start #配置的脚本文件rc.local为start
TimeoutSec=0
StandardOutput=tty #标准输出
RemainAfterExit=yes
SysVStartPriority=99 #优先级,当有多个开机启动文件时可以设置不同的值

[Install]
WantedBy=multi-user.target
1
2
# 生成rc.local
sudo nano /etc/rc.local
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
exit 0
1
2
3
4
5
# 给rc.local加上权限,启用服务,启动服务并检查状态
sudo chmod +x /etc/rc.local
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

不挂断后台启动

1
nohup command &

关闭

1
2
3
4
# 查看进程
jobs -l
# 杀死进程
sudo kill 7889