Score:1

Getting PID of dependency in systemd?

us flag
eof

I would like to write a systemd unit file that needs to pass to the exec command the PID of one of its dependencies. In the unit file I would like to have:

ExecStart=bar -p <PID of foo.service>
Requires=foo.service
After=foo.service

Is there a way to get the PID of a required dependency in some easy way? Couldn't locate anything in the manual nor an example in any of the unit files on an Ubuntu desktop.

The use case is to create a simple unit file that can be used to attach the JProfiler daemon to a Java based service in order to do remote performance debugging. The command to attach the profiler needs the PID of the JVM.

Score:0
cl flag
A.B

From man systemctl:

-p, --property=

When showing unit/job/manager properties with the show command, limit display to properties specified in the argument. The argument should be a comma-separated list of property names, such as "MainPID". [...]

So systemctl show -p MainPID foo will display MainPID=<pid> (rather than just the pid).

So if service foo can be identified by its Main PID (ie: the target process isn't actually a sub-process of the main pid), this could be used:

/bin/bash -c 'declare -i $(systemctl show -p MainPID foo); [ "$MainPID" -ne 0 ] && exec bar -p $MainPID || exit 1'

If not in its own script, with systemd's quoting conventions mangling a bit the line, this becomes:

ExecStart=/bin/bash -c 'declare -i $$(systemctl show -p MainPID foo); [ \"$$MainPID\" -ne 0 ] && exec bar -p $$MainPID || exit 1'

This script can be improved but you get the idea.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.