0

I have a script with this code:

#!/bin/bash
tmux new-session -d bash
    sleep 1d
    echo 1 day passed
    tmux kill-session

AFAIU, the echo will appear after 24 hours in the tmux session's stdout but not in my basic Bash session (the session I have after booting my operating system).

How could I make sure that the echo from the tmux session (which is ofcourse one layer above my basic Bash session), would be printed in my basic session?

The purpose is to utilize the sleep time phase in another session but to have output from all commands coming under it in my session.

Ofcourse, I can't use sleep in my base session because it would make the session unusable or "sleepy".

2

the echo will appear after 24 hours in the tmux session's stdout

If you refer to the tmux session you start from the script, then this is not true. bash process executing the script will wait for tmux new-session -d bash to exit, then it will execute sleep 1d and so on. Notice tmux new-session -d bash exits almost immediately because of -d (the other bash within the newly created tmux session doesn't exit but it's irrelevant now).

Change the delay to sleep 10 and run the script. Wait a while and you will see it echo-es to your current terminal. Next invoke tmux ls to see there's a new useless session left behind. You may attach to it and verify nothing was echo-ed there.


I can't use sleep in my base session because it would make the session unusable

So does your script, while running in foreground. How about a background job? You don't need tmux at all:

(sleep 20s; echo surprise) &

In general you can write to another terminal, if only you have proper permissions. The following is what I can do in my Debian. Let's say I want to write from session B, so the text is visible in bash session A. First I need to know what device the stdout of A actually is:

readlink /proc/$pid_of_bash_A/fd/1

From within the bash A this is simpler thanks to $$ special parameter:

readlink /proc/$$/fd/1

The output is /dev/pts/3 in my case. Then in the session B:

echo Hello World! > /dev/pts/3

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.