Is there any way to detect exiting a session on tmux? - tmux

I was wondering if there is a way to detect when I exit a tmux session.
It is common that I have multiple sessions in my workflow and I manage them with tmux-fzf plugin. Everything is good but there are some times when I am done with a session and want to exit it. after exiting, it would completely exit tmux and put me where I ran tmux command in the first place, and if I want to reattach to my sessions, I have to type tmux a. So ideally, I wanted to detect when I exit a session and in my tmux.conf file set a hook to check for existing tmux sessions and if there is attached to them instead of completely detaching from tmux.

Ok, I finally got it, by putting this line on the .tmux.conf file, Whenever a session is exited, the client will switch to the most recently active of the remaining sessions.
set -g detach-on-destroy off
As the explanation for this option on the tmux man page says :
If on (the default), the client is detached when the session it is attached to is destroyed. If off, the client is switched to the most recently active of the remaining sessions. If no-detached, the client is detached only if there are no detached sessions; if detached sessions exist, the client is switched to the most recently active.

Related

How to stop a workflow immediately when a session fails?

I have a workflow that has many sessions that run in parallel to each other. When one of the session fails, the workflow waits for the other session to complete and then the entire workflow gets failed. We have selected the option "fail parent if this task fails". But we want the workflow to fail and stop immediately if any of the session fails without waiting for other sessions to finish.
ps: We have a unix shell script that calls all the workflows one by one. So if we can solve it using unix shell scripting that would be fine aswell.
Does anyone have any solution for it?
Best thing you can do in Informatica is use a Control Task to Abort the worklfow, and have it connected from all sessions with an OR condition. Something like:
start--S1--S2--S3
\ \ \
\---\---\-(OR)-CTL

Is there a way to keep the bell active on current window (window name ends with !) until cleared?

On my local tmux session I connect remotely to another tmux session over ssh.
The only way to have the remote session inform the local tmux session of an event is to ring the terminal bell. In my local tmux window I keep track of the the remote session window name until I see the ! mark for the local session to respond to the event.
The problem I am facing is if the remote session was the active window and the event happened, the bell wont take effect and the window name wont end with !.
Is there a way to change that?
Basically keep the current window name ends with ! on a bell event until it gets cleared?

How do I load a particular line in a TMUX session?

The scrolling is soo slow in TMUX 2.7+. I have a TMUX session with a window that has 5,000 lines of code and I'd like to access a command that I sent around line 2,000. It would take forever to load it up.
I've tried going into command mode and putting in the number.
How do I access a specific line in a tmux session?
The goto-line command lets you jump directly to given line. If you use vi mode, it's :2000. If you use emacs mode, it's g2000.

How to start two or more tmux (or tmuxinator) sessions simultaneously?

I'm a huge fan of tmux + tmuxinator. Buy recently I found myself working with different sessions, and despite I configured'em with tmuxinator, I still need to open one at a time every day.
Is there a way to open two or more sessions at once so I can navigate between them as soon as I start tmux ot tmuxinator?
Check out this little mac os script I wrote:
#!/bin/bash
sessions=( session1 session2 )
for i in "${sessions[#]}"
do
osascript <<EOF
tell application "iTerm" to activate
tell application "System Events" to tell process "iTerm" to keystroke "t" using command down
tell application "System Events" to tell process "iTerm" to keystroke "tmuxinator start ${i}"
tell application "System Events" to tell process "iTerm" to key code 52
EOF
done

have R halt the EC2 machine it's running on

I have a few work flows where I would like R to halt the Linux machine it's running on after completion of a script. I can think of two similar ways to do this:
run R as root and then call system("halt")
run R from a root shell script (could run the R script as any user) then have the shell script run halt after the R bit completes.
Are there other easy ways of doing this?
The use case here is for scripts running on AWS where I would like the instance to stop after script completion so that I don't get charged for machine time post job run. My instance I use for data analysis is an EBS backed instance so I don't want to terminate it, simply suspend. Issuing a halt command from inside the instance is the same effect as a stop/suspend from AWS console.
I'm impressed that works. (For anyone else surprised that an instance can stop itself, see notes 1 & 2.)
You can also try "sudo halt", as you wouldn't need to run as a root user, as long as the user account running R is capable of running sudo. This is pretty common on a lot of AMIs on EC2.
Be careful about what constitutes an assumption of R quitting - believe it or not, one can crash R. It may be better to have a separate script that watches the R pid and, once that PID is no longer active, terminates the instance. Doing this command inside of R means that if R crashes, it never reaches the call to halt. If you call it from within another script, that can be dangerous, too. If you know Linux well, what you're looking for is the PID from starting R, which you can pass to another script that checks ps, say every 1 second, and then terminates the instance once the PID is no longer running.
I think a better solution is to use the EC2 API tools (see: http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ for documentation) to terminate OR stop instances. There's a difference between the two of these, and it matters if your instance is EBS backed or S3 backed. You needn't run as root in order to terminate the instance - the fact that you have the private key and certificate shows Amazon that you're the BOSS, way above the hoi polloi who merely have root access on your instance.
Because these credentials can be used for mischief, be careful about running API tools from a given server, you'll need your certificate and private key on the server. That's a bad idea in the event that you have a security problem. It would be better to message to a master server and have it shut down the instance. If you have messaging set up in any way between instances, this can do all the work for you.
Note 1: Eric Hammond reports that the halt will only suspend an EBS instance, so you still have storage fees. If you happen to start a lot of such instances, this can clutter things up. Your original question seems unclear about whether you mean to terminate or stop an instance. He has other good advice on this page
Note 2: A short thread on the EC2 developers forum gives advice for Linux & Windows users.
Note 3: EBS instances are billed for partial hours, even when restarted. (See this thread from the developer forum.) Having an auto-suspend close to the hour mark can be useful, assuming the R process isn't working, in case one might re-task that instance (i.e. to save on not restarting). Other useful tools to consider: setTimeLimit and setSessionTimeLimit, and various checkpointing tools (I have a Q that mentions a couple). Using an auto-kill is useful if one has potentially badly behaved code.
Note 4: I recently learned of the shutdown command in package fun. This is multi-platform. See this blog post for commentary, and code is here. Dangerous stuff, but it could be useful if you want to adapt to Windows. I haven't tried it, though.
Update 1. Three more ideas:
You could use .Last() and runLast = TRUE for q() and quit(), which could shut down the instance.
If using littler or a script that invokes the script via Rscript, the same command line functions could be used.
My favorite package of today, tcltk2 has a neat timer mechanism, called tclTaskSchedule() that can be used to schedule the execution of an expression. You could then go crazy with the execution of stuff just before a hourly interval has elapsed.
system("echo 'rootpassword' | sudo halt")
However, the downside is having your root password in plain text in the script.
AFAIK those ways you mentioned are the only ones. In any case the script will have to run as root to be able to shut down the machine (if you find a way to do it without root that's possibly an exploit). You ask for an easier way but system("halt") is just an additional line at the end of your script.
sudo is an option -- it allows you to run certain commands without prompting for any password. Just put something like this in /etc/sudoers
<username> ALL=(ALL) PASSWD: ALL, NOPASSWD: /sbin/halt
(of course replacing with the name of user running R) and system('sudo halt') should just work.

Resources