Score:0

Can another process signal systemd to restart a service?

fr flag

We have a service which is managed by systemd. I'm looking for a way to programatically restart that service on demand - for instance, by sending a signal, writing to a file, or messaging over a socket.

Context: the systemd managed service maintains a pool of headless chrome instances. But it manages them somewhat poorly, and over time they increase in number and memory usage. A systemctl restart fixes that.

I could set RuntimeMaxSec and Restart=always to restart the proxy periodically, but it might be doing work at the moment the restart occurs.

Another service on that machine is the only consumer of the chrome proxy, and knows when it's not using it. So periodically when it's not using the chrome proxy (so that no requests will be dropped), it could restart it.

How could I do that?

djdomi avatar
za flag
for what do you need chrome? curl and wget can do almost sone parts easily
fr flag
Headless Chrome can execute JavaScript, render the page and take screenshots, and more. Different tools for different purposes.
Score:0
fr flag

One solution is to have the other service actually run systemctl restart some_service. That requires sudo, but it's possible to use the sudoers file (visudo) to give a user permission to run one specific command: some-user ALL=NOPASSWD: /some/command some args

Score:0
br flag

If you want to avoid sudo, you can set your user's linger flag and create a user service instead of a system service. This might not work on centos 7 or below.

Another solution is to ask systemctl what the PID is

systemctl --user status your_scraper

then send a SIGHUP to that process id

os.kill(main_pid_of_your_scraper, signals.SIGHUP)

which you need to catch inside of your application and reset your browser.

signal.signal(signal.SIGHUP, restart_myself)

This also plays nicely with systemcl, since systemctl reload sends a SIGHUP signal to the process as well.

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.