rc.local 添加 多个 sh脚本
时间: 2024-01-22 12:38:41 浏览: 23
您可以将多个 sh 脚本的路径以空格分隔,放在 rc.local 文件中的相应位置。例如:
```bash
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
# Start multiple scripts
/bin/sh /path/to/script1.sh &
/bin/sh /path/to/script2.sh &
/bin/sh /path/to/script3.sh &
exit 0
```
请注意,在每个脚本的末尾使用 `&` 来使脚本在后台运行,以避免一个脚本阻塞 rc.local 中后续的脚本。
相关问题
开机启动rc.local和开机启动脚本
开机启动是指在系统启动时自动运行特定的脚本或程序。在 Linux 系统中,有多种方法可以实现开机启动,其中包括使用 rc.local 和编写开机启动脚本。
1. rc.local 方法:
rc.local 是一个在系统启动阶段自动执行的脚本文件,可以用来配置开机启动项。以下是使用 rc.local 实现开机启动的步骤:
1. 打开终端,使用文本编辑器(如 vi 或 nano)以管理员权限编辑 rc.local 文件:
```
sudo nano /etc/rc.local
```
2. 在文件中添加需要在系统启动时执行的命令或脚本,例如:
```
#!/bin/bash
# 启动脚本示例
/path/to/your/script.sh
```
注意:确保添加的命令或脚本的执行权限正确设置(如使用 chmod 命令设置为可执行)。
3. 保存并关闭文件。
4. 确保 rc.local 文件具有可执行权限:
```
sudo chmod +x /etc/rc.local
```
5. 重新启动系统,验证开机启动是否成功。
2. 开机启动脚本方法:
另一种常见的方法是编写一个专门的开机启动脚本,然后将其添加到系统的启动项中。以下是使用开机启动脚本方法的步骤:
1. 创建一个新的启动脚本文件,例如 `myscript.sh`:
```
#!/bin/bash
# 启动脚本示例
/path/to/your/script.sh
```
2. 保存并关闭文件。
3. 将脚本移动到 `/etc/init.d/` 目录中:
```
sudo mv myscript.sh /etc/init.d/
```
4. 为脚本设置执行权限:
```
sudo chmod +x /etc/init.d/myscript.sh
```
5. 将脚本添加到系统启动项中:
```
sudo update-rc.d myscript.sh defaults
```
6. 重新启动系统,验证开机启动是否成功。
请注意,具体步骤可能因不同的 Linux 发行版而有所差异。在某些发行版中,也可能使用 systemd 或其他工具来管理开机启动项。
rc.local 开机启动脚本
`rc.local` 是 Linux 系统中一个可以自定义的启动脚本,可以在系统启动的时候自动执行其中的命令,常用于一些需要在系统启动时就运行的服务或应用。
在 Ubuntu 系统中,`rc.local` 文件默认是存在的,路径为 `/etc/rc.local`,但是默认情况下是不启用的。如果你需要使用 `rc.local`,需要进行以下操作:
1. 确认 `rc.local` 是否存在并可执行:
```
ls -l /etc/rc.local
```
如果文件不存在或者没有可执行权限,需要进行以下操作:
```
sudo touch /etc/rc.local
sudo chmod +x /etc/rc.local
```
2. 编辑 `rc.local` 文件:
```
sudo nano /etc/rc.local
```
在其中添加需要在系统启动时执行的命令,例如:
```
#!/bin/bash
/path/to/your/script.sh
```
注意:在 `rc.local` 文件中添加的命令需要使用绝对路径,否则可能会出现路径问题导致无法执行。
3. 保存并退出 `rc.local` 文件,重启系统,命令将会自动执行。
注意:在使用 `rc.local` 的时候要注意权限问题,如果需要执行的命令需要 root 权限,需要在命令前加上 `sudo`。同时,如果需要等待网络等服务启动之后再执行命令,需要在命令前加上 `sleep` 命令等待一段时间。