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

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • 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. CentOS / RHEL 6 : How to boot into rescue mode
  2. How to Disable os-prober in CentOS/RHEL 7
  3. Why Does “/var/log/messages” Report Martian Packets
  4. How to Set External Network For Containers in Linux Containers (LXC)
  5. How to find the inode size of an ext2/ext3/ext4 filesystem?
  6. CentOS / RHEL : How to create a Thinly Provisioned Logical Volume
  7. How to enable bind query logging to find out Who’s Querying a Name Server
  8. What are Shell Scripts? How to Create Shell Scripts?
  9. Which network ports are reserved by the Linux Operating System?
  10. How To Add Standard Linux Users To Manage Print Jobs And Services in CentOS/RHEL

You May Also Like

Primary Sidebar

Recent Posts

  • How to Disable IPv6 on Ubuntu 18.04 Bionic Beaver Linux
  • How to Capture More Logs in /var/log/dmesg for CentOS/RHEL
  • Unable to Start RDMA Services on CentOS/RHEL 7
  • How to rename a KVM VM with virsh
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary