2

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

4

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.

  • I suspect the other problem was that ExecStop is supposed to be synchronous, i.e. the command specified needs to wait for the service to clean up before it returns. I'm not sure that quitting screen will wait for the contained processes to exit, it probably just closes the PTYs (causing SIGHUP to be sent). – sourcejedi Feb 11 '18 at 9:54
  • That's certainly possible. I've never tried using screen in a service context before, so I'm a little uncertain about how it interacts with systemd. – ErikF Feb 11 '18 at 11:00
  • I tried running the ExecStart command in one terminal and then the ExecStop command in another terminal but the process still running! – MOHAMMAD RASIM Feb 11 '18 at 11:33
  • It appears that the --shutdown option doesn't work in telnet mode. Assuming that the server always uses 57006 (that seems to be the default), I've edited the answer to send a command to gracefully shutdown the server rather than use systemd's termination routine. – ErikF Feb 11 '18 at 12:21
  • When i stop the service i get this in the journal 19:27:43 raspberrypi systemd[1]: biglybt.service: Control process exited, code=exited status=2 Feb 11 19:27:49 raspberrypi systemd[1]: Stopped BiglyBt daemon. Feb 11 19:27:49 raspberrypi systemd[1]: biglybt.service: Unit entered failed state. Feb 11 19:27:49 raspberrypi systemd[1]: biglybt.service: Failed with result 'exit-code'. and the service enters failed status – MOHAMMAD RASIM Feb 11 '18 at 16:29
0

Thanks to @ErikF and support from biglybt github account I was able to solve the issue, this service file now works for me

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

[Service]
Environment="DISPLAY=:0.0"
Type=simple
User=pi
Group=pi
ExecStart=/usr/bin/java -cp "/usr/share/java/jna.jar:/home/pi/biglybt/BiglyBT.jar:/home/pi/biglybt/dorkbox-systemtray.jar:/home/pi/biglybt/commons-cli.jar:/home/pi/biglybt/log4j.jar:/home/pi/biglybt/junit.jar:/home/pi/biglybt/swt.jar" -Djava.library.path=/home/pi/biglybt -Dbiglybt.install.path=/home/pi/biglybt -Dazureus.script=/home/pi/biglybt/biglybt -Dazureus.script.version=9 -Dazureus.overridelog=1 -Dazureus.overridelogdir=/home/pi/biglybtlogs/ -Duser.dir=/home/pi/biglybt com.biglybt.ui.Main --ui="console,telnet"
ExecStop=/usr/bin/java -cp "/home/pi/biglybt/BiglyBT.jar" com.biglybt.ui.Main --shutdown
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

Also I created a script to launch the swt(gui interface):

#!/bin/bash
/usr/bin/java -cp "/home/pi/biglybt/BiglyBT.jar" com.biglybt.ui.Main --ui=swt --open

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.