I am trying to create a xterm window and I don't want the user to close the window using 'X' button which led me to use the trap command.
xterm -e zsh -c "trap '' HUP INT TERM XFSZ; python"
The above will create a zsh and will start python process and the user can't close this xterm window.
I need to log the contents of the terminal. So, I have used script command.
xterm -e script mypgms.log -c "trap '' HUP INT TERM XFSZ;python"
But, the window can still be closed with 'X' button.
What should I do to have zsh started with python process and logging done via script command in xterm where user can't close it?
Related
I'm currently using mobaxterm to launch xterms to connect to a remote server. I use...
xterm screen
I use screen because the connection is unreliable so screen allows me to recover sessions.
What I really want is to call "screen -RR" to reconnect sessions if there, or start a new one, but xterm doesn't allow command line arguments.
I've played around with -e, -ls, -hold, etc but I can't get it to work.
Any ideas?
[Edit]
Additional information...
I've tried...
xterm ./script.sh
with screen -RR in it, but that runs, then exits. -l or -hold doesn't help.
xterm -e /bin/bash -c screen -RR
same problem, exits without giving me a prompt.
My current hack is...
xterm ./mybash
where mybash a sym-link to /bin/bash, and I have a check in .bashrc looking for XTERM_SHELL = mybash, then launching screen -RR, but that runs 2 bash shells, so I have to exit twice to close the window.
xterm -e should work. It takes one or more arguments specifying a command (plus its arguments) to execute under xterm (so it must be the last option).
For example, this should work
xterm -e screen -RR
There's no need to invoke /bin/bash to invoke screen.
Use case
I run command over a bastion in SSH and since the connection can be dropped we are using tmux or screen.
For short commands I'm using send-keys, let's say I'm using a command like kubectl get pods. I'd like to keep the output of this command but terminate the tmux session.
Using remain-on-exit is not what I'm looking for as it keeps a dead window.
I've seen those question already
https://unix.stackexchange.com/questions/17116/prevent-pane-window-from-closing-when-command-completes-tmux
TMUX: how to make new window stay when start shell-command quits?
I'd like to keep the output of this command but terminate the tmux session.
I use this in my ~/.tmux.conf, and now when I exit my running shell, pane output is saved to unique log file:
set -g remain-on-exit
set-hook pane-died 'capture-pane -S - -E - ; save-buffer "$HOME/logs/tmux/tmux-saved.#{host_short}-#{session_id}:#{window_id}:#{pane_id}-#{pane_pid}-#{client_activity}.log"; delete-buffer; kill-pane'
I'm not sure it's exactly what you are looking for, but you could use the capture-pane command to save the contents of a pane (subject to what is still in the pane's history) to a file.
Is there a way to show the command which is currently running at a tmux pane?
I tried 'history', but it does not seem to show the commands which I had executed at tmux.
I also tried 'ps -ef', but it does not show the full command in the case like "./a.sh ; ./b.sh"
I found several answers online that include ps ... | tail -1. Unfortunately, these don't always work because sometimes the order of the commands is swapped, e.g. for two separate panes I get:
$ ps -t /dev/pts/12 -o args=
-bash
mpv some_movie.mp4
$ ps -t /dev/pts/10 -o args=
micro some_file.txt
-bash
I really wanted a single line of output so that I could show it in the status bar, but what I ultimately ended up going with is ps --forest via run-shell. It seems to always reliably show the correct order and with more information should there be nested commands running (e.g. via a bash script). Its output looks like:
$ ps --forest -o args -g $$
COMMAND
-bash
\_ ps --forest -o args -g 1695
Solution
So in my .tmux.conf, I've got:
bind '`' run-shell 'ps --forest -o pid,args -g #{pane_pid}'
It will replace the contents of your pane with the output from the ps --forest command. Once you type esc or ^C, the ps output disappears, and your pane goes back to whatever it was doing :) Ends up looking like:
running script.sh, which calls other-script.sh, which sleeps for 30s
viewing pane process tree via keybinding
(Old question but for future reference)
Try: tmux list-panes -t <your_pane_name> -F '#{pane_current_command}'
https://man7.org/linux/man-pages/man1/tmux.1.html#FORMATS
Try setting pane-border-status to bottom or top in your configuration file, with the tmux command prompt or just running tmux set pane-border-status bottom. Borders should appear around the panes and info about the current process appears much like in a regular terminal window's title bar.
I suspect the command wasn't written to the history file as the shell with the stuck/long-running job wasn't done yet.
You might try pstree -U to see process in their parent/child tree.
Is there a general way to wait for an executed process that backgrounds in fish (like open "foo")? As far as I can tell, $! (the PID of the last executed child process in bash) is not present in fish, so you can't just wait $!.
1) The fish idiom is cmd1; and cmd2 or if cmd1; cmd2; end.
2) You should find that bash and zsh also don't block if you execute open ARG. That's because open will normally background the program being run then open exits. The shell has no idea that open has put the "real" program in the background. Another example of that behavior is launching vim in GUI mode via vim -g. Add the -W flag on macOS or -w on Linux to the open command and -f to the vim command.
The key here is that open, even if it backgrounds, won't return a signal that fish will use to evaluate the and operator until something happens to the opened process. So you get the behavior you're looking for.
Can I set tmux to trigger an alert in a non-active window when a process completes?
For example: I start a long build process. I would like to be notified when it completes, not each time it prints a status.
I'm surprised this answer hasn't been given yet: You can use the tmux window setting visual-bell for this. With bell-action you can then configure whether you want to see bells for the current window only, or for non-current window only (other). Personally I prefer the second, as you won't see noise generated by the shell, and you probably don't care about the notification if it's in the current window.
set-window-option -g visual-bell on
set-window-option -g bell-action other
When a process generates a bell, tmux will highlight the the title of the window that rings the bell as well as show a "Bell in window X" notification.
Then ring the bell at the end of the process. E.g.:
make; echo -e '\a'
(or && || instead of ; if you want to ring only on success or failure respectively)
There 3 solutions I know, none really ideal. You may put those commands in your ~/.tmux.conf or just run them directly as Tmux command via Ctrl-B :.
Monitor and alert whenever the output changes (you may then redirect output somewhere else so that output changes only after the command is complete):
:set -g visual-activity on
:setw -g monitor-activity on
Monitor and alert whenever the output did not change for a while (30 seconds here):
:set -g visual-silence on
:setw -g monitor-silence 30
(deprecated and someday replaced by a better option) Monitor and alert whevener the output contains a string matching given pattern (and possibly run your command like my-command; echo foobar):
:set -g visual-content on
:setw -g monitor-content foo*bar
$ some-command; tmux display-message "Task 1 done". However the message will only show for a short duration defined via :set -g display-time 4000.
If you keep the visual-* to off (default), it'll only highlight the name of the window in which the alert occurred without showing a global alert status message.
For details about each of these settings, see tmux man page
Updated (thanks for Steven Lu)
I finally found a solution that works for me. I use zsh for my shell, which has a feature called "Hook Functions" -- shell functions that execute on certain actions: http://zsh.sourceforge.net/Doc/Release/Functions.html.
It's likely other shells have a similar feature.
The hook function I use is precmd, which is executed each time the prompt is shown. ie, when a command has just finished running.
In my .zshrc:
precmd () {
echo -n -e "\a"
}
This sends a bell to tmux, which causes it to highlight just the window that the command was running in.
If you are already focused on that tmux window, this does nothing because the bell is immediately cleared.
The benefit of this approach is that it does not trigger on all visual activity. It only triggers when a command completes.
you can wrap your running script with a && bash derivative which will call a tmux command to notify you.
using && means you will only get notified when the script exits with error code 0. if you want it to notify you anyway, just use ;
as for the tmux commands to wrap the script, have a look at those bunch, they should suffice
select-window
split-window -h 'exec echo...'
send-keys
For future reference, people can also checkout the tmux-notify plugin, created by ChanderG, which was designed for this purpose.
Disclaimer: I'm a contributor to this plugin. Feel free to check it out and open an issue/pull request or feature request if you find a bug or see something that is missing.
there are two options:
set -g visual-activity on
setw -g monitor-activity on
have you tried setting them in your tmux.conf?
As a good workaround, you can use:
https://github.com/tcreech/tmux-notifications
You simply do:
$ command ; tmux-notify
You will get a nice notification in the status-bar (if enabled)
Building on #psp940's answer (where StackOverflow won't let me add a comment):
precmd () {
echo -n -e "\a" >$TTY
}
If you're using powerlevel10k's instant prompt feature, redirecting the bell directly to the terminal avoids a warning about pre-init console output. See also here.
Adding onto the answers from #wump and #psp940; I've found that the simplest way to be alerted any time a command completes is to put a bell in your PS1:
in tmux.conf
set-window-option -g visual-bell on
set-window-option -g bell-action other
In your .bashrc:
# ring bell every time a command completes
export PS1+=$'\[\a\]'
I'm not sure whether the += and $'' are bash-isms, but the principle will work in any shell.
The surrounding \[+\] tell your shell that the characters between them are zero-width