Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

linux - Systemctl(?) killing detached screens

I have a program written in Go that programmatically creates and manages screens. Here's an example:

_, err := exec.Command("screen", "-S", "screen-"+strings.ToLower(name), "-X", "stuff", command+"
").Output()

This works fine. When I Control+C the program, the screens created stay open (what I want!). However, I've converted it into a background service on Ubuntu. When I run systemctl stop <service>, it kills those screens without a warning. Attaching to a screen doesn't stop this either (instantly goes to [screen terminated]). It doesn't kill screens created externally, however.

Here is my .service:

[Unit]
Description=>servicename> background service
After=network-online.target

[Service]
ExecStart=/usr/lib/<servicename>/service

[Install]
WantedBy=multi-user.target

My rules:

%:
    dh $@ --with systemd --parallel

override_dh_auto_install:
    dh_auto_install
    dh_systemd_enable || true
    dh_systemd_start || true

My control:

Package: <name>
Version: 0.2
Architecture: amd64
Priority: optional
Maintainer: <me>
Description: <description>
Depends: screen, iptables
Build-Depends: dh-systemd (>=1.5)

I can't imagine what would cause these screens to kill. I'm pretty sure they're not being attached in the program because it runs fine as an executable. System logs don't mention anything outside of "service stopped' and 'service started'. I've tried making screens under root, different users, running an empty screen vs running a program, etc. Nothing notable.

Any ideas?

question from:https://stackoverflow.com/questions/65648532/systemctl-killing-detached-screens

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

systemd by default kills all processes (screens) if the main service that created them (your Go program) exits. Note this is not only subprocesses but any processes within the same cgroup. This is to make sure that if service crashes there are no leftover processes from it.

This behavior can be controlled using key KillMode= in .service unit file which is described here. Although not recommended you need to set it to either process or none (to leave your screens unmanaged and escape your service lifecycle management by systemd).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...