linux -/etc/init.d/something 和/etc/rc.local 到底有什么区别

标签 linux shell

<分区>

当我需要在 Ubuntu 中创建自动启动任务时。我总是创建一个新文件并向其中写入一堆命令。接下来,我将此文件放在/etc/init.d/目录中。然后,我为此文件设置了 chmod 755。最后,我执行命令行“update-rc.d file_name defaults”来激活它。它就像一个魅力。

最近,我发现还有另一种方法可以使它与上面的示例相同。那就是将一个新命令行附加到/etc/rc.local(将其放在“exit 0”行上方)。

那么你能说出它们之间的区别吗?非常感谢!

最佳答案

要理解这个问题,首先要知道的是*nix中的run level。 *nix 中共有 6 运行级别。我不会显示每个运行级别的详细信息,您可以阅读更多相关信息 here .

每个运行级别在 /etc/ 下都有单独的位置:

% cuonglm at ~
% ls -l /etc/rc* -d
drwxr-xr-x 2 root root 4096 Feb 20 10:44 /etc/rc0.d
drwxr-xr-x 2 root root 4096 Feb 20 10:44 /etc/rc1.d
drwxr-xr-x 2 root root 4096 Feb 20 10:44 /etc/rc2.d
drwxr-xr-x 2 root root 4096 Feb 20 10:44 /etc/rc3.d
drwxr-xr-x 2 root root 4096 Feb 20 10:44 /etc/rc4.d
drwxr-xr-x 2 root root 4096 Feb 20 10:44 /etc/rc5.d
drwxr-xr-x 2 root root 4096 Feb 20 10:44 /etc/rc6.d
-rwxr-xr-x 1 root root  306 Feb  4 18:58 /etc/rc.local
drwxr-xr-x 2 root root 4096 Feb  4 19:01 /etc/rcS.d

每次系统启动时,相应运行级别文件夹下的一些脚本(以S 开头)会被执行。即如果你启动到 run level 2/etc/rc2.d/ 下的一些脚本将被执行。如果显示这些文件夹的内容,您会看到脚本是 /etc/init.d/ 下脚本的符号链接(symbolic link)。

% ls -l /etc/rc2.d/
total 4
-rw-r--r-- 1 root root 677 Jul 27  2012 README
lrwxrwxrwx 1 root root  20 Feb 19 11:26 S20kerneloops -> ../init.d/kerneloops
lrwxrwxrwx 1 root root  27 Feb 19 11:26 S20speech-dispatcher -> ../init.d/speech-dispatcher
lrwxrwxrwx 1 root root  20 Feb 19 11:26 S50pulseaudio -> ../init.d/pulseaudio
lrwxrwxrwx 1 root root  15 Feb 19 11:26 S50rsync -> ../init.d/rsync
lrwxrwxrwx 1 root root  15 Feb 19 11:26 S50saned -> ../init.d/saned
lrwxrwxrwx 1 root root  19 Feb 19 11:26 S70dns-clean -> ../init.d/dns-clean
lrwxrwxrwx 1 root root  18 Feb 19 11:26 S70pppd-dns -> ../init.d/pppd-dns
lrwxrwxrwx 1 root root  14 Feb 19 11:26 S75sudo -> ../init.d/sudo
lrwxrwxrwx 1 root root  17 Feb 20 10:44 S91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root  22 Feb 19 11:26 S99acpi-support -> ../init.d/acpi-support
lrwxrwxrwx 1 root root  21 Feb 19 11:26 S99grub-common -> ../init.d/grub-common
lrwxrwxrwx 1 root root  18 Feb 19 11:26 S99ondemand -> ../init.d/ondemand
lrwxrwxrwx 1 root root  18 Feb 19 11:26 S99rc.local -> ../init.d/rc.local

这使您能够控制您的服务在哪个运行级别下运行。您可以让您的服务仅在 run level 2 运行并在其他运行级别停止。但请记住,只有一个“运行级别”在启动时执行,即运行级别 2 或 3 或 4 被执行,而不是 2 然后 3 然后 4。

因此它会引导您了解这里的不同之处。在你启动的每一个运行级别,执行该运行级别的脚本后,执行脚本/etc/rc.local。这意味着 /etc/rc.local 将在启动过程结束时运行,无论您启动的运行级别如何。

关于linux -/etc/init.d/something 和/etc/rc.local 到底有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21901385/

相关文章:

c - 如何使用 LD_PRELOAD 检查是否已预加载 linux 共享库

c - 在 Linux 中使用 C 中的几种 Ethtypes 接收以太网数据包

c++ - Qt pro文件的CXXFLAGS修改?

shell - 如何在 Unix shell 中将 pwd 定义为变量

shell - Shell 脚本中 SED 字符串中美元符号 @$VAR 之前的 at 符号有何作用?

linux - 注意变量变化

linux - mmap/dev/fb0 失败,返回 "Invalid argument"

php - 如何在linux中使用PHP调用另一个CLI.exe进程I/O?

shell - 如何在 unix shell 中使用数组?

linux - Unix 缓存二进制文件吗?