0

I've spent days going in circles with this one, my google skills have failed me...

I am using a systemd service to run a shell script on boot which should open a new tmux session and start a python program (that I would like to be able to keep running, and occasionally visit the tmux session via ssh)

Made a file called dropx.service:

sudo nano /lib/systemd/system/dropx.service

Changed attributes: (not sure if necessary)

sudo chmod 644 /lib/systemd/system/dropx.service

Contents of dropx.service:

[Unit]
Description=Start dropx.py in tmux session

[Service]
WorkingDirectory=/home/pi/drop/
User=pi
Type=idle
ExecStart=/bin/bash dropxtmux.sh #(some unit files have a 'START' here)
KillMode=process

[Install]
WantedBy=multi-user.target

This seems to work, I can control it with:

$ sudo systemctl start dropx.service

or

$sudo systemctl stop dropx.service

when I am happy it is working I can enable it at boot with:

$ sudo systemctl daemon-reload
$ sudo systemctl enable dropx.service

and can check on it with:

$ sudo systemctl status dropx.service
$ journalctl -u dropx.service

Here is the small shell script to start tmux and execute the python code:

#!/bin/bash
/usr/bin/tmux set-option -t dropx set-remain-on-exit on
#/usr/bin/tmux new-session dropx #tried different combinations of sessions and windows
/usr/bin/tmux new -s dropx
/usr/bin/tmux send-keys "python /home/pi/drop/dropx.py" Enter
exit 0 #(not sure if line is necessary either)

I have tried many variations of above, so far they all fail. Running just the script from terminal, it will start a new session but not start the python program, if you call it again, it will fail with errors, but start the program in the tmux session, leaving you with the desired result.

Last login: Mon Jan  7 11:53:13 2019
pi@whitepi:~ $ cd drop
pi@whitepi:~/drop $ sh dropxtmux.sh
error connecting to /tmp/tmux-1000/default (No such file or directory)
[detached (from session dropx)]

at this point, there is a tmux session created, but the python program is not running if I run the same command again:

pi@whitepi:~/drop $ sh dropxtmux.sh
duplicate session: dropx
pi@whitepi:~/drop $ tmux ls
dropx: 1 windows (created Mon Jan  7 11:56:34 2019) [85x57]
pi@whitepi:~/drop $ tmux attach
[detached (from session dropx)]

Any help would be much appreciated!

  • Is it required to use tmux? screen can do just the same. I ask because tmux has a level more on complexity. It works as client/server and starts a server. screen only starts a session. – Ingo Jan 8 at 0:07
  • it is not required, i've just altered the service to start the python program directly, I was just hoping to figure out this method – Travis Mcgee Jan 8 at 1:01
  • Is there much output from the python script? – Ingo Jan 8 at 9:12
  • no, <16kb/hour.. just status messages, I've figured out how to save those from rsyslog to a text file. – Travis Mcgee Jan 9 at 1:38
1

Not tested on the Pi but I have this in .bash_profile on another machine to start (or reconnect to) a tmux session with a couple of commands running in windows 0 and 5 and four terminals when I login:

if which tmux 2>&1 >/dev/null; then
    #
    # $TMUX variable is set when running in a session.
    #
    # If we're NOT in a session AND we fail to attach
    #  to the 'mysession' session THEN
    #    1. Start a new session with HTOP running
    #    2. Add 4 new windows with terminals
    #    3. Add a final new window watching the syslog
    #    4. Choose the first terminal window
    #    5. Connect to the new 'mysession' session
    # 
    test -z "$TMUX" && \
      ( tmux -2 attach-session -t mysession || \
        ( \
        tmux new-session -d -s mysession -n 'HTOP' '/usr/bin/htop'; \
        tmux new-window -t mysession:1 -n 'Term1'; \
        tmux new-window -t mysession:2 -n 'Term2'; \
        tmux new-window -t mysession:3 -n 'Term3'; \
        tmux new-window -t mysession:4 -n 'Term4'; \
        tmux new-window -t mysession:5 -n 'LOGS' '/usr/bin/watch tail -n 40 /var/log/syslog'; \
        tmux select-window -t mysession:1; \
        tmux -2 attach-session -t mysession \
        ) \
    )
fi

Not quite what you want (because this only starts the commands when I login the first time) but I think the key is you need to append your python command to the /usr/bin/tmux new -s dropx line in your script rather than use send-keys after.

  • Thank you very much, I will try playing with this tonight. I've seen multiple places where they show starting a tmux session and on the same line starting a python program in that window, for whatever reason, I can not get tmux to do this... I'll keep trying. – Travis Mcgee Jan 9 at 1:35
  • Had a thought when clarifying the code: have you tried it with the -d flag as well? This prevents tmux from grabbing the terminal when creating the new session. Without it the rest of your script might not be running 'till after you've exited the tmux session... – Roger Jones Jan 9 at 9:20
  • Ditto that the issue here is without -d tmux new-session is a blocking call. This may not be noticeable if you do it via a systemd service that itself goes to background -- in the background, it's still blocked. – goldilocks Jan 9 at 9:29
  • If I change the shell script to: #!/bin/bash tmux new-session -d -s dropx 'python /home/pi/drop/dropx.py' then the script works if called from the terminal, it opens a new tmux session and runs the python program inside, however, when I use a Systemd service to start the same shell script it fails to run. I am happy with having the service start the python program directly. Thanks for all your comments – Travis Mcgee Jan 10 at 5:11
  • @TravisMcgee You wrote: I am happy with having the service start the python program directly. Please make an answer to your solution. – Ingo Jan 11 at 12:19
1

As the questioner wrote in a comment he has solved the problem:

If I change the shell script to: #!/bin/bash tmux new-session -d -s dropx 'python /home/pi/drop/dropx.py' then the script works if called from the terminal, it opens a new tmux session and runs the python program inside, however, when I use a Systemd service to start the same shell script it fails to run. I am happy with having the service start the python program directly. Thanks for all your comments.

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.