systemctl status not showing CPU/Memory usage?

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?


Solution 1:

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].

Solution 2:

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

CPUAccounting = yes

MemoryAccounting = yes

Solution 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