“服务”和“/etc/init.d/”有什么区别?

Mar*_*ppi 123 upstart services

一段时间以来,我一直在管理 Ubuntu 风格上和下的服务器安装 - 我已经非常适应/etc/init.d/重新启动服务。现在我收到这条消息:

root@tatooine:~# /etc/init.d/mysql status
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mysql status

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the status(8) utility, e.g. status mysql
mysql start/running, process 14048
Run Code Online (Sandbox Code Playgroud)

这似乎是在 Ubuntu 的最新 LTS 中带来的 - 为什么?有什么不好?和/etc/init.d/之间有什么区别?service/etc/init.d/

txw*_*ger 113

/etc/init.d脚本是旧的做事方式。它们来自 System V 标准。但是,这些脚本仅以特定顺序触发,因此无法建立真正的依赖关系。

因此,开发 upstart 的目的是/etc/init.d用 upstart 脚本(在 中/etc/init)替换所有脚本。

service允许从/etc/init.d脚本平滑过渡到新贵脚本。将来,当越来越多的脚本转移到新贵时,服务仍然可以工作,因为它找到了两种可能性。

  • 一旦计划到位,systemd 将取代 upstart - 14.04 LTS 将使用 upstart。这是由 [Mark Shuttleworth](http://en.wikipedia.org/wiki/Mark_Shuttleworth) 在一篇题为 [Losing 优雅](http://www.markshuttleworth.com/archives/1316) 的帖子中宣布的 (7认同)
  • 新贵现在要被淘汰了,对吧? (6认同)

小智 29

还要检查服务命令的手册页: man service

service在可预测的环境中运行脚本(工作目录是 / 并且只设置了 2 个环境变量:LANG 和 TERM)。它还增加了做的能力--full-restart。所以总结一下:

  1. service 可以从 /etc/init 或 /etc/init.d(upstart 或 System V)运行脚本
  2. service 在可预测的环境中运行脚本。

如果您的脚本由于某种原因依赖于环境变量,则“可预测的环境”方面可能会给您带来问题。可能有办法解决这个问题,但我不知道它是什么,这超出了这个问题的范围:)

  • 嗨@Joe Marty,这正是我现在正在试验的问题。我有一个服务在“DISPLAY”环境变量上进行中继,在使用“service myservice start”启动守护程序时不存在该环境变量,但在使用“/etc/init.d/myservice start”启动它时存在。关于如何使用此变量的任何想法? (2认同)