ubuntu 开机启动脚本(亲测可用)

为了让程序更简单的运行,使用方便,开机启动已经成为了一种有效的方式。

ubuntu下的开机启动有时并不是很好用,需要自己调试总结经验。
下面介绍几种常用的开机启动方式:

第一种:创建独立的脚本文件 (该方法亲测可用,请优先尝试)

(1)编写脚本文件.sh

cd
vi run.sh

添加以下内容(注释也必须添加):

#!/bin/sh
### BEGIN INIT INFO
# Provides:          run.sh
# Required-start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the svnd.sh daemon
# Description:       starts svnd.sh using start-stop-daemon
### END INIT INFO
 
#任务脚本
#进入要执行脚本目录
cd /home/ubuntu/trx
#取得root权限,'123456'为密码,不用加引号,'ls'无实际作用
echo 123456|sudo -S ls
#执行脚本./bin/mywork,sudo -S需要加上
sudo -S ./bin/mywork
#任务脚本

任务脚本中,一般需要用到root权限,取得root权限,这里先做一下测试:
运行你写的脚本,看能否跑成功,成功的话继续进行下一步。

(2)将run.sh文件移到/etc/init.d目录下面

cp mysetup.sh /etc/init.d
sudo chmod 755 /etc/init.d/run.sh
cd /etc/init.d
sudo update-rc.d run.sh defaults 96

96代表启动的优先级,一般启动按照这个顺序执行,若是需要联网的程序运行时,建议设置大点(在连接网络的优先级后面)。
到此已经可以开机启动了:

sudo reboot

(3)卸载开机启动脚本

cd /etc/init.d
sudo update-rc.d -f mysetup.sh remove

第二种:利用rc.local添加启动脚本

rc.local脚本是一个ubuntu开机后会自动执行的脚本,我们可以在该脚本内添加命令行指令。该脚本位于/etc/路径下,需要root权限才能修改。系统不同可能存在位置不一样,有些在/etc/init.d/路径下,有时两个都存在,但是内层的会启动外层的,因此:

sudo vi /etc/rc.local

一般其内容为:


#!/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.
 
#任务脚本
#打开gnome终端,并在其中运行脚本
gnome-terminal -x /home/ubuntu/run.sh
#任务脚本
exit 0

注意:这种方法尝试了两次没有成功,小伙伴们自行选择吧。

第三种:随桌面启动
这种方法必须要求有界面的ubuntu才行,由于没有亲测,只提供有这个思路,具体请自行查找。

第四中方法:建立run.service服务机制。由于没有亲测,只提供有这个思路,具体请自行查找。(哈哈)
.service内容大致如下:

[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
 
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
 
[Install]
WantedBy=multi-user.target
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值