1

I want to start a tmux on boot on a raspberry pi - 2. I have written a session.sh file and I have places it in ~/bin/ file which is in the home directory. This ~/bin/ is also included in the $PATH and hence It can be triggered.

The Bash file looks like this:

#!/bin/sh

mySession(){
     tmux new-session -d -s SessionName
     tmux new-window -d -n 'windowName' "~/bin/myPythonScript.py"
}
mySession

exit 0

I have given execution rights to all scripts including the above mentioned bash script too using chmod +x session.sh

In the rc.local I refer to trigger this file as follows:

#!/bin/sh
scriptSession(){
        echo "starting the tmux session"
        ~/bin/session.sh &
}

scriptSession
exit 0

I have tried rebooting my Pi many times but the session never gets triggered.

when I execute sudo tmux ls, it just shows error Connecting to Server

however when I do sudo /etc/rc.local or sudo ~/bin/session.sh the session starts! I do not want this to happen since I want automatic start on boot not a user triggered session

Any Idea what could be the problem?

Edit

I also tried collecting a fail log in a bash subshell using thing the following in the rc.local file:

(scriptSession)&>>/var/log/myLog.txt

but upon cat /var/log/myLog.txt nothing shows up and the script still fails

2
  • I have just noticed: in your rc.local there is scriptSession function defined but never run. Yet you wrote "when I do sudo /etc/rc.local (...) the session starts". Does it really? – Kamil Maciorowski Jul 6 '16 at 12:43
  • Hi , I actually do call the function ! there is a typo ! will fix it. but it still doesn't work – Shan-Desai Jul 6 '16 at 13:24
1

The most optimized solution to troubleshoot any detached script using tmux will require you to use the following option within your triggering script:

#!/bin/bash
# this script is called "sess"

tmux new-session -d -s sess1

# this statement is a life-saver for tmux detached sessions
tmux set-option -t sess1 set-remain-on-exit on

# In my case running the script in a new window worked
tmux new-window -d -n 'nameofWindow' -t sess1:1 'sudo /home/pi/bin/script.py'

exit 0

Now the following script was called from the rc.local and the Pi was rebooted. Eventually on reboot when you attach the session using sudo tmux a Once gets a tmux session with 2 windows

  1. Initial one is just an empty session triggered due to tmux new-session -d -s sess1

  2. and the another one from the tmux new-window command which can be opened using CTRL+B + 1 since it was mentioned as sess1:1 (note: Hot keys may vary for user, the default tmux hotkey (bindkeys) are CTRL+B)

Inference

If the script ends with an error, the Window will show you where the error was in my case errors in my Python script and at the Bottom it will show Pane is Dead. Hence due to errors in the script the tmux session was exited without giving any relevant log(feedback) hence no output was logged in the above mentioned /tmp/tmux.log

Hence it is always recommended using the set-remain-on-exit on when running scripts with tmux in case if there are faults in the the script in detached mode

0

I think rc.local environment during startup isn't rich. If it knows any $PATH and $HOME (shell expands ~ to $HOME) they are not your (regular user's) variables; I think they may be unset. Furthermore the script will inherit this limited environment.

However when you do sudo /etc/rc.local or sudo ~/bin/session.sh the command inherits your environment and all works.

To fix it use full path instead of ~/. Use full path to tmux also.

5
  • alright you mean /usr/bin/tmux ? – Shan-Desai Jun 21 '16 at 19:05
  • @Shan-Desai If that's where your tmux executable is – then yes. – Kamil Maciorowski Jun 21 '16 at 19:06
  • Alright will have a try and get back to you thanks @Kamil – Shan-Desai Jun 21 '16 at 19:10
  • this still isn't working? any more hints? – Shan-Desai Jul 6 '16 at 12:18
  • finally figured out the problem @Kamil – Shan-Desai Jul 8 '16 at 20:31

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.