Autostart a process on GNU screen with systemd

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. – http://www.gnu.org/software/screen/

systemd is a system and service manager for Linux, compatible with SysV and LSB init scripts. – http://freedesktop.org/wiki/Software/systemd/

For our purpose, we are gonna target rtorrent as an example of the auto start using systemd.

To install screen on Ubuntu

 sudo apt-get install screen 

To install screen on ArchLinux

 sudo pacman -S screen

We will need to create a file with our desired name of the service on /etc/systemd/system/

sudo YOUREDITOR /etc/systemd/system/rtorrent@.service

Fill the document with:

[Unit]
Description=rTorrent

[Service]
Type=forking
User=USERNAME
ExecStart=/usr/bin/screen -d -m -S rtorrent /usr/bin/rtorrent
ExecStop=/usr/bin/killall -w -s 2 rtorrent
WorkingDirectory=/home/USERNAME

[Install]
WantedBy=multi-user.target

Now that’s just adding the service, we need to enable it so it runs at every boot, notice the ExecStop line, that will kill the rTorrent when the service is going off while ExecStart will start it detached with session name rtorrent.

To enable the service:

sudo systemctl enable rtorrent@.service

As soon as you reboot, rtorrent will be started on a detached screen

gnuscreen

If you want reattach the screen:

screen -R rtorrent

To detach:
Press Control + a then d

For further information and examples, check out https://wiki.archlinux.org/index.php/Systemd/Services

One thought on “Autostart a process on GNU screen with systemd”

Leave a Reply