3

I would like to start a python script at startup inside a byobu/tmux session (and create the session if it doesn't exist), so I can attach to it later.

I created a start_script.sh containing the following command:

byobu-tmux new-session -A -s userscript \; rename-window userscript1 \; send-keys "cd /home/username/scripts/ && python userscript1.py" C-m

It works fine when executed manually. I then created a systemd service file

[Unit]
Description=Sensors service
After=multi-user.target
StartLimitIntervalSec=0

[Service]
Type=forking
User=username
ExecStart=/bin/bash /home/username/start_script.sh
RemainAfterExit=yes

[Install]
WantedBy=default.target

However, the service fails with

Jun 21 17:11:39 hostname bash[15061]: open terminal failed: not a terminal

I also tried to put the start_script command after ExecStart=, but starting the service fails with the same error message (just byobu-tmux as command name instead of bash).

Ideally, I'd love to have the python process handled by systemd as a service, so it automatically restart on-failure, but that's not a requirement.

Julian S.
  • 133
  • it appears to me like you need systemd to allocate a terminal to your service; presumably byobu is the one complaining about the lack of a terminal. – Jeff Schaller Jun 21 '19 at 18:41

2 Answers2

4

Here is what finally worked for me (on Ubuntu 18.04).

The /home/username/start_script.sh (don't forget the chmod +x for that file):

#!/bin/bash
set -x
set -e

byobu list-sessions | grep my-app || byobu new-session -d -s my-app

byobu list-windows -t my-app | grep start-script || byobu new-window -t my-app -n 'start-script'

byobu send-keys -t my-app:start-script "cd /home/username/scripts/ && python userscript1.py" C-m

And the /etc/systemd/system/my-app.start_script.service:

[Unit]
Description=My app start script

[Service]
Type=forking
ExecStart=/bin/bash -l -c '/home/username/start_script.sh'
User=username
Group=usergroupname

[Install]
WantedBy=multi-user.target

Then install it thanks to sudo systemctl enable my-app.start_script.service.

To see the logs of the service: sudo journalctl -u my-app.start_script.service.

Thanks to this answer, this one and this one for the hints.

  • 2
    It didn't work for me out of the box (I could start the service and all would be fine, but it wouldn't work after reboot). However, changing Type=oneshot to Type=forking did the trick for me ! Thanks – Julian S. Aug 30 '19 at 19:01
  • I confirm add 'Type=forking' work – Tiana987642 Dec 18 '21 at 03:18
0

Do you have #/bin/bash as the first line in your script? Not sure if that would solve it or not, but that way you can just call your script on the ExecStart entry.