2

I enabled some Accounting features in systemd daemob:

DefaultCPUAccounting=yes
DefaultIOAccounting=yes
DefaultMemoryAccounting=yes
DefaultTasksAccounting=yes

Now I can see, for example, the service memory usage:

● supervisor.service - Supervisor process control system for UNIX
     Loaded: loaded (/lib/systemd/system/supervisor.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-02-21 15:10:05 UTC; 17min ago
       Docs: http://supervisord.org
   Main PID: 1842113 (supervisord)
      Tasks: 886 (limit: 108515)
     Memory: 31.9G

How can I get the "31.9G" value?

I can parse the text, of course, but I want to know if there is any systemd command to get that value that will simplify the bash parse.

I found this command:

root@panel:~# systemctl show supervisor | egrep -i memorycurrent
MemoryCurrent=34285473792

a Probably is the best command to check that. I just want to check if someone has something better. I want to use systemd because I need to understand the memory used by the daemon and the threads.

1
  • 2
    You can use the -p flag with systemctl show to show a specific property. For example: systemctl show -p MemoryCurrent supervisor. No difference in the output, just another way of narrowing the results down without using another command. Commented Feb 21, 2022 at 15:37

1 Answer 1

1

If you want a systemd solution that is compatible with dbus, you can:

$ busctl introspect \
   org.freedesktop.systemd1 \
   /org/freedesktop/systemd1/unit/supervisor_2eservice \
   org.freedesktop.systemd1.Service

This will give you a list of all Service properties of your service. When you know what property is interesting, you can simplify your command and ask for that specific property:

$ busctl get-property \
   org.freedesktop.systemd1 \
   /org/freedesktop/systemd1/unit/supervisor_2eservice \
   org.freedesktop.systemd1.Service \
   MemoryCurrent

This will return the following to stdout:

t 34285473792

The preceding t is the type-specification. It says that this is a UINT64.

If you want to "parse" it you could pipe that into awk or use -j to get json data and pipe that into jq:

$ busctl get-property \
   org.freedesktop.systemd1 \
   /org/freedesktop/systemd1/unit/supervisor_2eservice \
   org.freedesktop.systemd1.Service \
   MemoryCurrent | awk '{print $2}'
34285473792

$ busctl get-property \
   org.freedesktop.systemd1 \
   /org/freedesktop/systemd1/unit/supervisor_2eservice \
   org.freedesktop.systemd1.Service \
   MemoryCurrent -j | jq '.data'
34285473792

The org.freedesktop.systemd.Unit interface may be more appropriate if you are looking for things like active status or dependencies, but I think the specific information you described here is only found on the org.freedesktop.systemd.Service interface.

The dbus interface is a nice low-level interface with systemd. If you ever wanted to code something up in C, you'll be interfacing with systemd using this same interface (same service, object, interface, and property listed here)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.