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 <serviceName> 写入
[Service]
Environment="key1=Value1"
Environment="key2=Value2"

https://serverfault.com/questions/413397/how-to-set-environment-variable-in-systemd-service
2021.01.05补充 -bash: fork: retry: Resource temporarily unavailable | cgroup: fork rejected by pids controller,不只可以用来添加环境变量,更可以用来覆盖默认的配置,其实本来就是用来改配置的,sudo systemctl status user-1000.splice 如果有override就可以看到Cgroup比如要修改用户的Tasks限制,那么就sudo systemcltl edit --force user-1000.slice 覆盖默认配置,- -force很重要

[Slice]
TasksMax=infinity

然后再执行 systemctl daemon-reload
默认的为max,在ubuntu 18.04 上为10813个任务
【这个配置还能添加啥参数和Linux任务调度粒度得学习下】
systemclt还可以查看服务的环境变量
(UID可以通过id来获取)
参考1 参考2 参考34
看了下systemctl的文档,似乎用edit --full比较好,它拷贝了原来的配置,如果用--force,原来的一些初始化逻辑会被跳过,drop-in,比如- -full下的编辑配置如下:

[Unit]
Description=User Slice of jimmy
After=systemd-logind.service
After=systemd-user-sessions.service
[Slice]
TasksMax=10813

以上是完全错误的理解
--full不会创建drop-in files drop-in file 在 /etc/systemd/system/XXXunit.d目录下,可以有多个drop-in文件,修改你关心的配置,
--full是完全覆盖原来的配置,它对应的文件就是直接配置文件 /etc/systemd/system/user-1000.slice
又有
1). hexdump -C 来看 cat /proc/pid/environment 文本的内容,看到一个00,这就是C中字符串结尾是\0
2).模糊记得个tr,不知道怎么用,翻了半原来是 tr \\0 \\n
3). 还是strings方便 strings /proc/pid/environment

  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/
补充一个维基的截图(有生之年系列!我要给中国移动点一个大大的赞)
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值