创建限制 systemd 服务文件

创建限制 systemd 服务文件

我想为我的 systemd 服务设置一些限制,例如:

  1. 网络带宽
  2. CPU报价

如果超过限制,则 systemd 服务应该停止。

先感谢您

答案1

man systemd.exec:

   PROCESS PROPERTIES
   LimitCPU=, LimitFSIZE=, LimitDATA=, LimitSTACK=, LimitCORE=, LimitRSS=, LimitNOFILE=, LimitAS=,
   LimitNPROC=, LimitMEMLOCK=, LimitLOCKS=, LimitSIGPENDING=, LimitMSGQUEUE=, LimitNICE=,
   LimitRTPRIO=, LimitRTTIME=
       Set soft and hard limits on various resources for executed processes. See setrlimit(2) for
       details on the resource limit concept. Resource limits may be specified in two formats:
       either as single value to set a specific soft and hard limit to the same value, or as
       colon-separated pair soft:hard to set both limits individually (e.g.  "LimitAS=4G:16G").
       Use the string infinity to configure no limit on a specific resource. The multiplicative
       suffixes K, M, G, T, P and E (to the base 1024) may be used for resource limits measured in
       bytes (e.g.  "LimitAS=16G"). For the limits referring to time values, the usual time units
       ms, s, min, h and so on may be used (see systemd.time(7) for details). Note that if no time
       unit is specified for LimitCPU= the default unit of seconds is implied, while for
       LimitRTTIME= the default unit of microseconds is implied. Also, note that the effective
       granularity of the limits might influence their enforcement. For example, time limits
       specified for LimitCPU= will be rounded up implicitly to multiples of 1s. For LimitNICE=
       the value may be specified in two syntaxes: if prefixed with "+" or "-", the value is
       understood as regular Linux nice value in the range -20..19. If not prefixed like this the
       value is understood as raw resource limit parameter in the range 0..40 (with 0 being
       equivalent to 1).

对于带宽,您可以在中定义几个选项man systemd.resource-control.service.socket.slice.scope.swap、 或.mount单位可以使用以下任何一个:

IOWeight=weight, StartupIOWeight=weight
    Set the default overall block I/O weight for the executed processes, if the 
    unified control group hierarchy is used on the system. ...

根据 kernel.org 的IO接口文件

权重范围为 [1, 10000],指定 cgroup 与其同级组相比可以使用的相对 IO 时间量。

IOReadBandwidthMax=device bytes, IOWriteBandwidthMax=device bytes
    Set the per-device overall block I/O bandwidth maximum limit for the executed 
    processes, if the unified control group hierarchy is used on the system. This 
    limit is not work-conserving and the executed processes are not allowed to use 
    more even if the device has idle capacity. ...
IOReadIOPSMax=device IOPS, IOWriteIOPSMax=device IOPS
    Set the per-device overall block I/O IOs-Per-Second maximum limit for the 
    executed processes, if the unified control group hierarchy is used on the 
    system. This limit is not work-conserving and the executed processes are not 
    allowed to use more even if the device has idle capacity. ...

相关内容