Systemd – Stop Systemd Service by Sending Command to Screen Session

gnu-screensystemd

I run Biglybt as a daemon inside a screen session using the following command

screen -c /home/pi/.screenconf -L -dmS Biglybt_screen /usr/bin/java -cp "/usr/share/java/jna.jar:/home/pi/biglybt_stock/BiglyBT.jar:/home/pi/biglybt_stock/dorkbox-systemtray.jar:/home/pi/biglybt_stock/commons-cli.jar:/home/pi/biglybt_stock/log4j.jar:/home/pi/biglybt_stock/junit.jar:/home/pi/biglybt_stock/swt.jar" -Djava.library.path=/home/pi/biglybt_stock -Dbiglybt.install.path=/home/pi/biglybt_stock -Dazureus.script=/home/pi/biglybt_stock/biglybt -Djava.net.preferIPv4Stack=true -Ddebug.swtexec=1 -Dazureus.config.path=~/.biglybt_stock  com.biglybt.ui.Main --ui=console

And I can stop the daemon by sending quit command to the screen session as below

screen -S Biglybt_screen -p 0 -X stuff "quit ^M"

now I want to make a service file so that when the system shutdown the screen session gets a quit command to be exited gracefully

I tried this service file

[Unit]
Description=BiglyBt daemon
After=network-online.target

[Service]
Type=oneshot
User=pi
RemainAfterExit=yes
ExecStart=/usr/bin/screen -c /home/pi/.screenconf -L -dmS Biglybt_screen /usr/bin/java -cp "/usr/share/java/jna.jar:/home/pi/biglybt_stock/BiglyBT.jar:/home/pi/biglybt_stock/dorkbox-systemtray.jar:/home/pi/biglybt_stock/commons-cli.jar:/home/pi/biglybt_stock/log4j.jar:/home/pi/biglybt_stock/junit.jar:/home/pi/biglybt_stock/swt.jar" -Djava.library.path=/home/pi/biglybt_stock -Dbiglybt.install.path=/home/pi/biglybt_stock -Dazureus.script=/home/pi/biglybt_stock/biglybt -Djava.net.preferIPv4Stack=true -Ddebug.swtexec=1 -Dazureus.config.path=~/.biglybt_stock  com.biglybt.ui.Main --ui=console
ExecStop=/usr/bin/screen -S Biglybt_screen -p 0 -X stuff "quit ^M"

[Install]
WantedBy=multi-user.target

but when I stop the service, the process just gets killed instead of ending the screen session, So what am I missing?

UPDATE:
This service file works

[Unit]
Description=BiglyBt daemon
After=network-online.target

[Service]
Environment=DISPLAY=0.0
Type=simple
User=pi
Group=pi
ExecStart=/usr/bin/java -cp "/home/pi/biglybt_stock/BiglyBT.jar:/home/pi/biglybt_stock/swt.jar" -Djava.library.path=/home/pi/biglybt_stock -Dbiglybt.install.path=/home/pi/biglybt_stock -Dazureus.script=/home/pi/biglybt_stock/biglybt -Dazureus.config.path=/home/pi/.biglybt_stock -Dazureus.overridelog=1 -Dazureus.overridelogdir=/home/pi/biglybtlogs/ -Ddebug.swtexec=1 com.biglybt.ui.Main --ui="console,telnet"
ExecStop=/usr/bin/java -cp /home/pi/biglybt_stock/BiglyBT.jar -Djava.library.path=/home/pi/biglybt_stock -Dbiglybt.install.path=/home/pi/biglybt_stock -Dazureus.script=/home/pi/biglybt_stock/biglybt -Dazureus.config.path=/home/pi/.biglybt_stock  com.biglybt.ui.Main --shutdown
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target

The problem now is that i can no longer start the swt ui(gui) of biglybt.
The swt ui can be started either by telneting to the server and issue ui swt
or running

/usr/bin/java -cp "/home/pi/biglybt_stock/BiglyBT.jar:/home/pi/biglybt_stock/swt.jar" -Djava.library.path=/home/pi/biglybt_stock -Dbiglybt.install.path=/home/pi/biglybt_stock -Dazureus.script=/home/pi/biglybt_stock/biglybt -Dazureus.config.path=/home/pi/.biglybt_stock  -Dazureus.overridelog=1 -Dazureus.overridelogdir=/home/pi/biglybtlogs/ -Ddebug.swtexec=1 com.biglybt.ui.Main --ui="swt" --open

which will send request to the main process to start the swt ui.
Unfortunately this runs fine inside regular terminal window but when running the main process inside systemd then this give

Unable to init server: Could not connect: Connection refused

inside the journal logs

Best Answer

You probably don't want to use screen to daemonize inside of systemd because systemd assumes certain things about how a process works, particularly in oneshot mode. From the systemd.service(5) documentation:

Behavior of oneshot is similar to simple; however, it is expected that the process has to exit before systemd starts follow-up units. RemainAfterExit= is particularly useful for this type of service. This is the implied default if neither Type= nor ExecStart= are specified.

Your process isn't exiting immediately, so oneshot isn't the correct behaviour to look for.

Looking at bigly --help:

usage: [options] [torrent [torrent ...]]
 -h,--help        Show this help.
 -u,--ui <uis>    Run <uis>. ',' separated list of user interfaces to run
                  (swt, console, telnet). The first one given will respond
                  to requests without determinable source UI (e.g. further
                  torrents added via command line).
    --closedown   shutdown an existing instance of BiglyBT
    --shutdown    shutdown an existing instance of BiglyBT
    --open        show the BiglyBT interface
    --share       share a resource

Bigly is able to start in telnet mode, which should be good enough to run as its own daemon without any extra help; it can then talk to a running instance to send a shutdown command using --shutdown. Taking this into account, we can run the service in simple mode (I excluded classpath references and command line options that weren't required to run, so add them back if you need them):

bigly.service:

[Unit]
Description=BiglyBt daemon
After=network-online.target

[Service]
Type=simple
User=pi
ExecStart=/usr/bin/java -cp /home/pi/biglybt_stock/BiglyBT.jar -Djava.library.path=/home/pi/biglybt_stock -Dbiglybt.install.path=/home/pi/biglybt_stock -Dazureus.script=/home/pi/biglybt_stock/biglybt -Dazureus.config.path=/home/pi/.biglybt_stock  com.biglybt.ui.Main --ui=telnet
#ExecStop=/usr/bin/java -cp /home/pi/biglybt_stock/BiglyBT.jar -Djava.library.path=/home/pi/biglybt_stock -Dbiglybt.install.path=/home/pi/biglybt_stock -Dazureus.script=/home/pi/biglybt_stock/biglybt -Dazureus.config.path=/home/pi/.biglybt_stock  com.biglybt.ui.Main --shutdown
#SuccessExitStatus=143
ExecStop=/bin/sh -c "nc 127.0.0.1 57006 <<< 'quit iamsure'"

[Install]
WantedBy=multi-user.target

The process exits with an exit code of 143, so I noted that as the success condition for the service. As --shutdown doesn't seem to work in telnet mode, I used netcat to send the quit command to the telnet server (port 57006 appears to be the default.) As well, there were a number of error conditions at startup, but I was looking at getting the program running so I ignored them.

The telnet interface binds to all interfaces, so you may want to set up a firewall rule to prevent outside connections.

Related Question