ubuntu开机自动启动脚本_Ubuntu 下添加开机启动脚本

2023-05-16

Ubuntu下添加开机启动脚本

本文介绍在Ubuntu下添加开机启动脚本的两种方法:

1.编辑 /etc/rc.local文件

Ubuntu 会在启动时自动执行 /etc/rc.local文件中的脚本,默认该文件中有效的脚本代码为空,把需要执行的脚本添加到该文件的exit 0 之前即可,举例如下:

#!/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.

cd /home/ubuntu

echo 'hello,world' >> rc.local.log

exit 0

2.通过 update-rc.d命令添加开机自启动脚本

Ubuntu 服务器在启动时会自动执行 /etc/init.d目录下的脚本,所以我们可以将需要执行的脚本放到/etc/init.d目录下,或者在该目录下创建一个软件链接指向其他位置的脚本路径,然后通过update-rc.d将脚本添加到开机自启动。启动脚本必须以 #!/bin/bash 开头。举例如下:

新建开机启动脚本start_when_boot,放置到/etc/init.d 目录

#!/bin/bash

### BEGIN INIT INFO

# Provides: steven_qin

# Required-Start: $local_fs $network

# Required-Stop: $local_fs

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: self define auto start

# Description: self define auto start

### END INIT INFO

wget -q -O- https://oa.xxxx.net/cron/index

修改权限sudo chmod 755 /etc/init.d/start_when_boot

执行 update-rc.d start_when_boot defaults将上述脚本添加为开机启动;

执行update-rc.d -f start_when_boot remove将上述开机启动脚本移除;

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ubuntu开机自动启动脚本_Ubuntu 下添加开机启动脚本 的相关文章

随机推荐