方法一:编辑rc.loacl脚本
rc.local是一个常用的开机启动脚本,它会在是在系统初始化级别脚本运行后运行,因此可以比较安全地在里面添加那些在系统启动之后执行的脚本。
rc.local的路径是/etc/rc.local
#!/bin/bash
#
# 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.
echo Hello World
mkdir /home/ubuntu/rew
echo asdasda >> /home/ubuntu/try.log
cd /home/ubuntu
./new.sh
exit 0
把你想添加的启动项放在exit 0之前
** 注意 **
- 在Ubuntu 16以后系统没有/etc/rc.local了,需要新建一个rc.local文件
touch /etc/rc.local sudo vi /etc/rc.local
- 把rc.local添加在启动项里面
sudo chown root:root /etc/rc.local sudo chmod 755 /etc/rc.local sudo systemctl enable rc-local.service
常见问题
-
The unit files have no installation config (WantedBy, RequiredBy, Also, Alias settings in the [Install] section, and DefaultInstance for template units). This means they are not meant to be enabled using systemctl.
编辑/etc/systemd/system/py_service.service文件
[Install]
WantedBy=multi-user.target
[Unit]
After=network.target
[Service]
Type=simple
ExecStart=(...)service.py
ExecReload=(...)service.py
Restart=always
开启service
sudo systemctl daemon-reload
systemctl enable py_service.service
方法二: 添加 /etc/init.d
将你的启动脚本复制到 /etc/init.d目录
mv test /etc/init.d/test
sudo chmod 755 /etc/init.d/test
将该脚本放倒启动列表中去
cd .etc/init.d
sudo update-rc.d test defaults 95
其中数字95是脚本启动的顺序号。当有多个脚本的时候,可以设定启动的顺序。