• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to control resource (cgroup) with systemd for user process group in CentOS/RHEL 7

by admin

The Basics

RHEL7 moves the resource management settings from the process level to the application level by binding the system of cgroup hierarchies with the systemd unit tree. The old way of configuring cgroup, by means of editing several /etc/cg*.conf files is still available but no longer recommended.

Systemd provides three unit types(slice, scope, service) that are used for the purpose of resource control.

Handling cgroup with service unit of systemd

1. Customize a service unit for your service in /etc/systemd/system, if your service don’t have systemd service unit. You need to make a start/stop script to manage your service.

# cat /etc/systemd/system/mytask.service
[Unit]
Description= **
After=remote-fs.target nss-lookup.target

[Service]
ExecStart=/root/start_process.sh
ExecStop=/root/stop_process.sh

[Install]
WantedBy=multi-user.target

2. Control the resource via command line(take control the cpu quota 60% as sample)

# systemctl set-property mytask CPUQuota=60%

By default, it only support following attributes via command set-property.

AccuracySec=            CPUQuota=               KillMode=               LimitLOCKS=             LimitRTPRIO=            SendSIGHUP=
BlockIOAccounting=      CPUShares=              KillSignal=             LimitMEMLOCK=           LimitRTTIME=            SendSIGKILL=
BlockIODeviceWeight=    DefaultDependencies=    LimitAS=                LimitMSGQUEUE=          LimitSIGPENDING=        User=
BlockIOReadBandwidth=   DeviceAllow=            LimitCORE=              LimitNICE=              LimitSTACK=             WakeSystem=
BlockIOWeight=          DevicePolicy=           LimitCPU=               LimitNOFILE=            MemoryAccounting=       
BlockIOWriteBandwidth=  Environment=            LimitDATA=              LimitNPROC=             MemoryLimit=            
CPUAccounting=          Group=                  LimitFSIZE=             LimitRSS=               Nice=      

3. If you want to set the attribute which is not listed as above, you can use the following method.

# echo 70 > /sys/fs/cgroup/memory/system.slice/httpd.service/memory.swappiness

Or control the resource via modifying the service unit files

# cat /etc/systemd/system/mytask.service
[Unit]
Description= **
After=remote-fs.target nss-lookup.target

[Service]
CPUQuota=90%
MemoryLimit=1500000
ExecStartPre=/bin/bash -c '/bin/echo 70 > /sys/fs/cgroup/memory/system.slice/httpd.service/memory.swappiness'
#ExecStartPost
ExecStart=/root/start_process.sh
ExecStop=/root/stop_process.sh

[Install]
WantedBy=multi-user.target

4. Reload systemd manager configuration and start your service

# systemctl daemon-reload   
# systemctl start mytask

Filed Under: CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. How to move /usr and /var to Another Partition or Disk
  2. CentOS / RHEL 7 : How to Modify GRUB2 Arguments with grubby
  3. grub-install Command Examples in Linux
  4. rm Command Examples in Linux
  5. goaccess Command Examples in Linux
  6. logsave Command Examples in Linux
  7. How to Remove/ Disable Bash shell Command History on Linux
  8. ldconfig Command Options
  9. mt: command not found
  10. traceroute Command Examples in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • nixos-rebuild Command Examples in Linux
  • nixos-option: Command Examples in Linux
  • nixos-container : Command Examples in Linux
  • nitrogen Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright