20200226 systemctl service systemD runlevel

  1. service命令和systemctl命令的关系
  2. Linux系统服务会开机自启吗?如何给服务添加环境变量
  3. 文件夹/etc/rc0.d/etc/rc5.d,代表着什么?为什么既有/etc/init.d又有/etc/init文件夹

答:

  1. ubuntu 14上有service,如man所述,serivce运行SystemV脚本,都是/etc/init.d下的脚本

service runs a System V init script or upstart job in as predictable an environment as possible, removing most environment variables and with the current work‐
ing directory set to /.
The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT, or the name of an upstart job in /etc/init.

systemctl是较新的,在ubuntu14.04上都没有这个命令,它是SystemD上管理service的方法
https://askubuntu.com/questions/911525/difference-between-systemctl-init-d-and-service

  1. 只要这个服务是enable的,它就会开机自启,ubuntu14.04下/etc/rc0.d有readme文件,说不想让他开机自启可以重命名
    给服务添加环境变量
    systemctl edit 写入
[Service]
Environment="key1=Value1"
Environment="key2=Value2"

https://serverfault.com/questions/413397/how-to-set-environment-variable-in-systemd-service

  1. 代表不同runlevel下要执行的脚本,K代表Kill S代表Start,ubuntu14.04下/etc/rc0.d有readme,ubuntu 18.04 http://manpages.ubuntu.com/manpages/bionic/en/man8/runlevel.8.html;runlevel也已经过时咯

“Runlevels” are an obsolete way to start and stop groups of services used in SysV init.
systemd provides a compatibility layer that maps runlevels to targets, and associated
binaries like runlevel. Nevertheless, only one runlevel can be “active” at a given time,
while systemd can activate multiple targets concurrently, so the mapping to runlevels is
confusing and only approximate. Runlevels should not be used in new code, and are mostly
useful as a shorthand way to refer the matching systemd targets in kernel boot parameters.
Table 1. Mapping between runlevels and systemd targets

   ┌─────────┬───────────────────┐
   │Runlevel │ Target            │
   ├─────────┼───────────────────┤
   │0        │ poweroff.target   │
   ├─────────┼───────────────────┤
   │1        │ rescue.target     │
   ├─────────┼───────────────────┤
   │2, 3, 4  │ multi-user.target │
   ├─────────┼───────────────────┤
   │5        │ graphical.target  │
   ├─────────┼───────────────────┤
   │6        │ reboot.target     │
   └─────────┴───────────────────┘

init.d和init的历史:
init是kernel启动后第一个执行的程序
sysVint在linux由来已久,debian继承了它,它会执行init.d下的脚本;然后ubuntu又推出来一个upstart来管理系统服务,upstart兼容了sysVInit脚本,它读取init下的配置;现在又出来个systemD来代替sysVint,见如下提问,systemD怎么使用/etc/init/d下的脚本,通过systemd-sysv-generator
https://askubuntu.com/questions/5039/what-is-the-difference-between-etc-init-and-etc-init-d
https://unix.stackexchange.com/questions/233468/how-does-systemd-use-etc-init-d-scripts
https://www.linux.com/news/introduction-services-runlevels-and-rcd-scripts/
补充一个维基的截图(有生之年系列!我要给中国移动点一个大大的赞)
20200226 systemctl service systemD runlevel_第1张图片

你可能感兴趣的:(Unix/Linux,追杀)