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
$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!