tmuxp icon indicating copy to clipboard operation
tmuxp copied to clipboard

Commands in shell_command do not run as expected

Open ksalman opened this issue 6 years ago • 10 comments

Step 1: Provide a summary of your problem

  • For general technical questions, problems or feature requests related to the code in this repository file an issue.

Step 2: Provide tmuxp details

  • Python version 2.7.6

  • system PATH /u/ksalman/.pyenv/plugins/pyenv-virtualenv/shims:/u/ksalman/.pyenv/shims:/u/ksalman/.pyenv/bin:/u/ksalman/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/u/qa/tools:/usr/X11R6/bin:/u/qa/lab/scripts:/u/ksalman/scripts

  • tmux version tmux 2.7-rc

  • tmuxp version tmuxp 1.5.1

  • tmux path /usr/local/bin/tmux

  • tmuxp path, /u/ksalman/.pyenv/shims/tmuxp

  • libtmux version libtmux==0.8.1

  • shell GNU bash, version 4.3.30(1)-release (i586-pc-linux-gnu)

  • output of tmux list-sessions, tmux list-windows, tmux list-panes [ksalman@dalaran:~] $ tmux list-sessions 0: 18 windows (created Thu Jun 7 14:41:45 2018) [105x53] (attached) smoot: 7 windows (created Tue Feb 26 10:14:18 2019) [129x42]

[ksalman@dalaran:~] $ tmux list-windows 0: bash- (2 panes) [105x53] [layout eb9a,105x53,0,0[105x26,0,0,0,105x26,0,27,937]] @0 1: openstack (2 panes) [105x53] [layout e033,105x53,0,0[105x21,0,0,2,105x31,0,22,3]] @1 2: ssh (2 panes) [105x53] [layout 7b0d,105x53,0,0{23x53,0,0,194,81x53,24,0,195}] @106 3: aptly (1 panes) [105x53] [layout 6f0b,105x53,0,0,232] @120 4: solaris-oak-vcs2960 (1 panes) [105x53] [layout 6f0e,105x53,0,0,273] @134 5: jiracurl (1 panes) [105x53] [layout 6f0f,105x53,0,0,445] @188 6: bash (3 panes) [105x53] [layout bfd2,105x53,0,0[105x21,0,0,448,105x31,0,22{28x31,0,22,459,76x31,29,22,460}]] @190 7: bash (2 panes) [105x53] [layout f507,105x53,0,0[105x21,0,0,487,105x31,0,22,488]] @203 8: bash (1 panes) [105x53] [layout 2f0d,105x53,0,0,514] @218 9: bash (3 panes) [105x53] [layout 1c1b,105x53,0,0{31x53,0,0,517,73x53,32,0[73x21,32,0,550,73x31,32,22,552]}] @219 10: emeril (3 panes) [105x53] [layout 6ecd,105x53,0,0[105x21,0,0,766,105x31,0,22{45x31,0,22,749,59x31,46,22,833}]] @279 11: bash (2 panes) [105x53] [layout a1a5,105x53,0,0{45x53,0,0,838,59x53,46,0,839}] @308 12: bash (1 panes) [105x53] [layout ef0b,105x53,0,0,850] @311 13: bash (1 panes) [105x53] [layout ef0d,105x53,0,0,852] @312 14: (1 panes) [105x53] [layout ef13,105x53,0,0,858] @314 15: bash (1 panes) [105x53] [layout d7b7,105x53,0,0,1023] @396 16: bash (1 panes) [105x53] [layout d7be,105x53,0,0,1049] @405 17: vi* (2 panes) [105x53] [layout 1fb2,105x53,0,0[105x26,0,0,1089,105x26,0,27,1109]] @423 (active)

[ksalman@dalaran:~] $ tmux list-panes 0: [105x26] [history 0/50000, 0 bytes] %1089 1: [105x26] [history 0/50000, 0 bytes] %1109 (active)

  • output of tmux show-options -g, tmux show-window-options -g [ksalman@dalaran:~] $ tmux show-options -g activity-action other assume-paste-time 1 base-index 0 bell-action any default-command "" default-shell "/bin/bash" destroy-unattached off detach-on-destroy on display-panes-active-colour red display-panes-colour blue display-panes-time 1000 display-time 750 history-limit 50000 key-table "root" lock-after-time 0 lock-command "lock -np" message-command-style fg=yellow,bg=black message-style fg=black,bg=yellow mouse off prefix C-a prefix2 None renumber-windows on repeat-time 500 set-titles off set-titles-string "#S:#I:#W - "#T" #{session_alerts}" silence-action other status on status-interval 15 status-justify left status-keys vi status-left "[#S] " status-left-length 10 status-left-style default status-position bottom status-right " "#{=21:pane_title}" %H:%M %d-%b-%y" status-right-length 40 status-right-style default status-style fg=black,bg=green update-environment[0] "DISPLAY" update-environment[1] "SSH_ASKPASS" update-environment[2] "SSH_AUTH_SOCK" update-environment[3] "SSH_AGENT_PID" update-environment[4] "SSH_CONNECTION" update-environment[5] "WINDOWID" update-environment[6] "XAUTHORITY" visual-activity off visual-bell off visual-silence off word-separators " -_@"

[ksalman@dalaran:~] $ tmux show-window-options -g aggressive-resize off allow-rename off alternate-screen on automatic-rename on automatic-rename-format "#{?pane_in_mode,[tmux],#{pane_current_command}}#{?pane_dead,[dead],}" clock-mode-colour blue clock-mode-style 24 force-height 0 force-width 0 main-pane-height 24 main-pane-width 80 mode-keys vi mode-style fg=black,bg=yellow monitor-activity off monitor-bell on monitor-silence 0 other-pane-height 0 other-pane-width 0 pane-active-border-style fg=green pane-base-index 0 pane-border-format "#{?pane_active,#[reverse],}#{pane_index}#[default] "#{pane_title}"" pane-border-status off pane-border-style default remain-on-exit off synchronize-panes off window-active-style default window-status-activity-style reverse window-status-bell-style reverse window-status-current-format "#I:#W#{?window_flags,#{window_flags}, }" window-status-current-style bg=blue window-status-format "#I:#W#{?window_flags,#{window_flags}, }" window-status-last-style default window-status-separator " " window-status-style default window-style default wrap-search on xterm-keys on

  • output of tmuxp freeze <SESSION_NAME>

Step 3: Describe your environment

  • Architecture: i686
  • OS version: Debian 8.0

Step 4: Describe the problem:

Steps to reproduce:

Observed Results:

  • What happened? This could be a description, log output, etc. Commands in shell_command appear to be running before the shell is initialized. When I load the config pasted below, I get this in the pane created:
 cd /var/log
 echo foo
[ksalman@dalaran:~] $  cd /var/ echo

Expected Results:

  • What did you expect to happen? I would expect commands to run and the output would look like this:
[ksalman@dalaran:~] $ cd /var/log
[ksalman@dalaran:/var/log] $ echo foo
foo

Relevant Code:

session_name: test
windows:
- window_name: test
  panes:
     - shell_command:
         - cd /var/log
         - echo foo

ksalman avatar Mar 07 '19 16:03 ksalman

@ksalman I did not try replicating your environment, but from a quick test on my setup (Python 3.9, tmuxp 1.7.0, tmux 3.0a), I get the expected output from the example session yaml that you attached. Are you still having an issue with this? (I know it has been a while since you have opened this issue, but didn't want to close it without resolution).

joseph-flinn avatar Jan 18 '21 18:01 joseph-flinn

I saw a similar issue as above, where I found that the shell_commands were not being executed (and it appeared as if a tab key was being inserted after the first command - a tab complete listing was coming up - perhaps related?). The behaviour is consistent across multiple machines running a variety of (old-ish, 2.1-2.9) versions of tmux. I had seen similar problems with my own bash script for launching tmux sessions, where I fixed it by adding in a sleep 10 call. I noticed that recent versions of tmuxp has a similar sleep_before option (undocumented?). For me this fixes my problem:

session_name: test
windows:
- window_name: test
  panes:
     - shell_command:
         - cd /var/log
         - echo foo
         sleep_before: 0.5

N.B. I think this is applied on a per-pane basis. It would be good if you could do this per-window (or even per-session), although I'm not sure if this is currently possible. Currently, this causes quite a lengthy wait for tmux to start up.

markmuetz avatar Mar 28 '22 11:03 markmuetz

N.B. I think this is applied on a per-pane basis. It would be good if you could do this per-window (or even per-session), although I'm not sure if this is currently possible.

I think it would be fine if you made an issue for this. It sounds like a good idea

tony avatar Mar 28 '22 11:03 tony

Fixing bugs by inserting random sleep is definitely not a good idea…

piotr-dobrogost avatar Mar 29 '22 21:03 piotr-dobrogost

@piotr-dobrogost What do you propose?

tony avatar Mar 29 '22 23:03 tony

I'm not familiar with the internals of the project and can only say what many others would say; we should investigate the problem and find the reason for this race. If the reason is that shell is not initialized as suggested earlier it seems the solution is to find a way to wait for shell's initialization to be completed before running commands.

piotr-dobrogost avatar Mar 30 '22 09:03 piotr-dobrogost

@piotr-dobrogost I agree in principle. Adding a sleep is definitely a workaround. It does work though. I am happy to investigate further or provide info on my tmux setup, but this does affect me on multiple computers. For me, and for the time being, adding in a short sleep is acceptable.

It seems like what is needed is either polling the shell to see if it accepting commands, or a callback that is triggered after the shell has been initialized. However, I don't know about the internals well enough (or the intersection between tmux/bash/tmuxp/libtmux) to know if either of these is possible.

markmuetz avatar Mar 30 '22 13:03 markmuetz

Googling for tmux wait for shell brings this Tmux in bash script executing too fast. The title is probably not quite right, the answer (needlessly?) uses sleep but what is interesting is that they use tmux wait-for which maybe could be a better workaround than plain sleep.

piotr-dobrogost avatar Mar 30 '22 14:03 piotr-dobrogost

@piotr-dobrogost @markmeutz

tmuxp isn't in control of shell commands themselves finishing. that is why sleep exists.

there may be ways to do such a thing but having it done in a portable + simple fashion is another question.

  • a smarter wait: we may be able to delay a command until pane output . #96

    we may be able to hook into a string match of libtmux.Pane.capture_pane() to see if a command completes

  • tmux wait-for

    seems worth making an issue for in libtmux. It'd be perfect fit for a context manager.

    https://github.com/tmux-python/libtmux/issues/366#issue-1188206306

    i don't know yet whether it helps here though. it depends on the user

    The sleep that tmuxp does isn't a sending keys to sleep, it's time.sleeping in the workspace builder.

tony avatar Mar 31 '22 14:03 tony

It seems like what is needed is either polling the shell to see if it accepting commands, or a callback that is triggered after the shell has been initialized. However, I don't know about the internals well enough (or the intersection between tmux/bash/tmuxp/libtmux) to know if either of these is possible.

These may be possible in the future but would need to be organized into their own issues

tony avatar Mar 31 '22 14:03 tony