11

I used Ubuntu 16.04. And I find some services will show cpu and memory usage via systemctl status <name>.service:

$ systemctl status nginx
● nginx.service - LSB: Stop/start nginx
   Loaded: loaded (/etc/init.d/nginx; bad; vendor preset: enabled)
   Active: active (running) since 五 2017-04-07 09:21:25 CST; 4h 59min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 2677 ExecStart=/etc/init.d/nginx start (code=exited, status=0/SUCCESS)
    Tasks: 2
   Memory: 2.5M
      CPU: 12ms
   CGroup: /system.slice/nginx.service
           ├─2695 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.con
           └─2697 nginx: worker process

But on another host, I find the systemctl status will not show cpu and memory usage:

$ systemctl status nginx
● nginx.service - LSB: Stop/start nginx
   Loaded: loaded (/etc/init.d/nginx; bad; vendor preset: enabled)
   Active: active (running) since 四 2017-04-06 20:57:15 CST; 17h ago
     Docs: man:systemd-sysv-generator(8)
   CGroup: /system.slice/nginx.service
           ├─29668 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.con
           ├─29669 nginx: worker process                   
           ├─29670 nginx: worker process                   
           ├─29671 nginx: worker process                   
           └─29672 nginx: worker process

Why? Both of the nginx were installed from nginx official repo apt install -y nginx. Even though on the same host, some services will not show cpu and memory usage.

And how to show the cpu and memory usage in systemctl status?

muru
  • 197,895
  • 55
  • 485
  • 740
Feng Yu
  • 877
  • 3
  • 8
  • 17
  • After some experimentation I have concluded that this happens on hosts that are upgraded to 16.04. For fresh 16.04 installs both memory and CPU are displayed but not when upgrading from 14.04 so either there are some config done on 14.04 that prevents this or some package that is missing when one upgrades. – Henrik Holst May 24 '17 at 21:55
  • Yes, you're right! But I don't know which package could show theses. – Feng Yu May 25 '17 at 01:39
  • same here, no idea from the source code, I am using systemd 219 from centos7. – georgexsh Jul 19 '18 at 11:22

3 Answers3

11

I have no idea why some hosts could but some couldn't, if you want a consistent behavior, you should enable memory accounting for a single unit or by default for all units by setting:

DefaultMemoryAccounting=yes

in /etc/systemd/system.conf and then doing:

systemctl daemon-reexec

Check out this list thread from the systemd developers, and systemd-system.conf[5].

georgexsh
  • 226
  • Yes you're right. And I find that if I use do-release-upgrade to upgrade from 14.04 to 16.04, even though I'm not add DefaultMemoryAccounting=yes to /etc/systemd/system.conf, the systemctl status will show cpu and memory usage. – Feng Yu Jul 30 '18 at 12:32
  • On Amazon Linux 2, the issue that caused Memory to stop displaying on systemctl status xxx.service (despite it seeming to work correctly at the kernel level) was caused by running systemctl enable xxx.service after starting the service. – Lawrence Wagerfield Jan 20 '23 at 00:34
7

Sometimes it might help adding these 2 options in your <myapp>.service file under the [Service] block

CPUAccounting=yes
MemoryAccounting=yes
Welz
  • 127
  • 2
  • 2
  • 6
3

Its actually less complicated than you think. To see something non-static like CPU or memory usage, you have to use the command: watch systemctl status <name>.service