• 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. logwatch : command not found
  2. macchanger: command not found
  3. How to create partitions and file systems on DM-Multipath devices
  4. Understanding How an Email System Works
  5. Setting up an NFS server with Turnkey Linux
  6. Tutorial on Linux Clustering (High Availability)
  7. bashmarks: Save and jump to commonly used directories using 1 character commands
  8. getfacl Command Examples in Linux
  9. pacman –query Command Examples
  10. netselect: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • cheat: Create and view interactive cheat sheets on the command-line
  • chars: Display names and codes for various ASCII and Unicode characters and code points
  • chafa: Image printing in the terminal
  • cf: Command-line tool to manage apps and services on Cloud Foundry

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright