centos 系统
1.直接修改rc.local把启动程序的命令添加到/etc/rc.d/rc.local文件中
ubuntu18.04设置
- 通过服务开机启动
ls /lib/systemd/system | grep rc
修改原有的服务
sudo vi /lib/systemd/system/rc.local.service
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
创建
#!/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 "this shows rc.local is start onboot" > /usr/local/rc-local-info.log
# add your scritp here
exit 0
为rc.local加执行权限:
sudo chmod +x /etc/rc.local
sudo ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
sudo systemctl enable rc-local -- 启用服务
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
添加新的服务启动脚本的方式
创建名为my-server的服务:
sudo vi /etc/systemd/system/my-server.service
写入以下信息
启动脚本=aaa.sh
启动命令=aaa.sh start
[Unit]
Description=启动脚本路径 Compatibility
ConditionPathExists=启动脚本路径
[Service]
Type=forking
ExecStart=启动命令
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
然后创建你得aaa.sh
sudo chmod +x /etc/aaa.sh
sudo systemctl enable my-server-- 启用服务
sudo systemctl start my-server.service
sudo systemctl status my-server.service
以上开机启动都是 以root 执行 ,注意权限问题 ,执行有问题,请添加打印日志排查你得命令