参考:https://www.cnblogs.com/downey-blog/p/10473939.html
目录
方案一:rc.local
- 使用cat /etc/rc.local查看原始信息如下
- 添加启动项
直接在/etc/rc.local 的空白位置添加需要启动的应用。如下:
说明:path标识应用的全路径、appname表示要启动的应用名称、&标识后台启动
方案二:init.d
- 编写启动脚本
### BEGIN INIT INFO
# Provides: downey
# Required-Start: $local_fs $network
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: tomcat service
# Description: tomcat service daemon
### END INIT INFO
# Required-Start: 运行这个脚本需要的环境
# Required-Start: 停止这个脚本需要的环境
# Default-Start: 提供运行的运行级别
# Default-Stop: 不运行的运行级别
# Description: 描述
#!/bin/bash
path+appname
说明:path标识应用的全路径、appname表示要启动的应用名称、&标识后台启动
- 添加可执行权限
chmod +x test.sh
- 复制到/etc/init.d下
sudo cp test.sh /etc/init.d/
- 将test.sh脚本链接到开机运行序列中
cd /etc/init.d
sudo update-rc.d test.sh defaults 96
- 从开机序列中移除
sudo update-rc.d -f test remove
方案三:systemd
- 编写test.service
[Unit]
Description=
Documentation=
After=network.target
Wants=
Requires=
[Service]
ExecStart=/home/downey/test.sh
ExecStop=
ExecReload=/home/downey/test.sh
Type=simple
[Install]
WantedBy=multi-user.target
# Description:运行软件描述
# Documentation:软件的文档
# After:因为软件的启动通常依赖于其他软件,这里是指定在哪个服务被启动之后再启动,设置优先级
# Wants:弱依赖于某个服务,目标服务的运行状态可以影响到本软件但不会决定本软件运行状态
# Requires:强依赖某个服务,目标服务的状态可以决定本软件运行。
# ExecStart:执行命令
# ExecStop:停止执行命令
# ExecReload:重启时的命令
# Type:软件运行方式,默认为simple
# WantedBy:这里相当于设置软件,选择运行在linux的哪个运行级别,只是在systemd中不在有运行级别概念,但是这里权当这么理解。
- 赋值test.service到启动目录下
sudo cp test.service /usr/lib/systemd/system
或者
sudo cp test.service /etc/systemd/system
或者
sudo cp test.service /lib/systemd/system
- 启用服务
sudo systemctl enable test.service
- 启动服务
sudo systemctl start test.service
方案四:图形化界面自启动
- test.desktop快捷方式编写
快捷文件存储在/usr/share/applications/下
[Desktop Entry]
Encoding=UTF-8
Name=MATLAB %桌面快捷方式的名称
Comment=MATLAB %注记
Exec=/usr/local/bin/matlab %可执行文件完整路径,用户主目录不可使用~代替
Icon=/usr/local/MATLAB/R2016b/toolbox/shared/dastudio/resources/MatlabIcon.png %图标文件完整路径
Terminal=true %是否使用终端,ubuntu下的matlab使用时需要调用终端
StartupNotify=true
Type=Application
Categories=Application;Development;
- 添加到自启动
ln -s /usr/share/applications/test.desktop ~/.config/autostart/