ubuntu中编写shell脚本开机自动启动(推荐)

1.进入终端输入

创建一个后缀为.sh的脚本文件

touch test.sh

编辑脚本

vim test.sh

test.sh编写脚本内容

2.编写test.sh脚本

 #!/bin/bash

### BEGIN INIT INFO
# Provides:     test
# Required-Start:  $remote_fs $syslog
# Required-Stop:   $remote_fs $syslog
# Default-Start:   2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: start test
# Description:    start test
### END INIT INFO

#此处编写脚本内容
cd /home/Desktop/
./test.sh
exit 0

     a. 以#!/bin/bash开头 中间写脚本内容 exit 0 结尾

     b.注释信息不可少,不然不生效

3.移动test.sh文件到/etc/init.d目录下

移动文件

sudo mv test.sh /etc/init.d/

保存成功以后,设置文本权限,否则不生效

chmod 755 test.sh

3、将命令脚本添加到启动脚本中去

sudo update-rc.d test.sh defaults

4、如需移除自启动,运行以下命令

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

 

5.这样一个开机自动化脚本就完成咯
posted @ 2024-03-08 15:30  zhihua  阅读(878)  评论(0编辑  收藏  举报