How to start a new process in a new window in an existing GNU screen session, from outside the session - session

I asked a similar question here and got pretty close to what I'm looking for
How to start a process from another application and then open a terminal to that process in gnu screen
...but, this doesn't quite get to what I'm after, which is starting a process in a new window (not an existing window) in an existing screen session (not a new screen session).
If you are at a command prompt in a screen session, you can do this:
screen [screen options] command param1 param2
But, how do you do it if you are outside the session? For example, if you want to run this as a cronjob?

When you aren't in a screen session, you can pass the -X flag to screen to execute a screen command.
For example, the screen command in screen opens a new window, optionally with a command to execute.
Assuming you already have screen open somewhere:
screen -dr -X screen sleep 20
Note that the new window will close once the command (sleep in this case) finishes. If you want to keep it open you could potentially do something like this, which will type 'date\n' in the new window:
screen -dr -X screen
screen -dr -X stuff "date
"
The man pages of course have much more detail.

Related

New iTerm tab with command from bash script

I am trying to automate a few things in the morning when I get to work, like start the docker env, start the front end stuff etc...
I want to type in a terminal command that will open up a terminal tab (iterm) for each of these tasks.
I have managed to get the tabs to open, but I cannot figure out how to make them run commands.
#!/bin/bash
open -a iTerm . "start_docker.sh"
However, this just opens up a black terminal window.
Ideally, I would like to not also have separate script files.

How can I see a preview of a tmux session/window from outside of tmux?

While inside of tmux, I can press PREFIX w, which runs tmux choose-tree.
This brings up an interactive session/window/pane selector that also displays a
preview of the highlighted node.
From a normal shell outside of tmux, I can run tmux attach -t '$1:1' to attach
to any session or window by name or index (see man tmux section "Clients And
Sessions").
Is there command similar to tmux attach -t where I can pass in a session/window
name/index and see a preview of it, like to the one used in tmux choose-tree? Ideally this command would just show the lower portion of this
image:
The goal here is to pipe a list of tmux windows from all sessions to
fzf, and pass a separate command in fzf's
--preview flag to show a preview of the session/window currently highlighted
while fuzzy finding.
I have a draft working
here.
It populates a preview with the session/window index in a tmux-compliant format
and will switch to the selected session.
This can be partially implemented by means of this recent commit: https://github.com/tmux/tmux/commit/2628af573d98f7bdd4db02c7a80c860b867a45fb
I do so here: https://github.com/petobens/dotfiles/commit/c21c306660142d93d283186210ad9d301a2f5186 in order to get the following:

Wait for screen window to end

I'm writing a shell script which utilises screen to display several things at once (split screens)
One of the windows will be the "gui" code which will ask the user questions while other windows display various progress items.
Part way through the script I have a need to vim a file. But this part of the code is within a subshell with redirected inputs. vim does not work well here*
So I figured I could open up a new screen window and load vim in there screen -X screen vim file
The problem is I need the gui code to wait until vim has quit before continuing.
So how can I wait for a specific screen window to end, from within screen?
screen -ls will not work here because it is only one of several windows within the whole session which I need to monitor.
I've also looked at screen -QX windows but this would appear to interrupt the vim session
* Relating to vim and the subshell: I've tried using exec 3<&1; cat file1 | ( read whatever; vim file2 <&3 ) but this has some major issues (e.g. can't see the first few lines of the file!)
Based on #William's comment this code:
screen -X screen vim --servername MYSERVER`
sleep 1
vim --servername MYSERVER --remote-wait file.txt
opens up vim in a separate window (and focusses it)
and then opens the file into that session and waits until the user quits vim
The sleep would appear to be required to give them vim session time to start up, otherwise the 2nd command can't find the vim server and instead tries to edit locally (which won't work inside the redirected subshell)

Mac OS X: Bring (non-bundle) GUI applications to foreground when launched from the command line

When a GUI process is launched from the OS X terminal, the window shows up in the background, and you have to use command-tab to give it focus.
Is there a way to make the terminal automatically give such GUIs focus after they are launched?
For example (assuming gitk is installed):
% gitk
should launch the GUI and then switch to it.
Note: For several reasons, using open as this answer suggests is not a general solution.
Update: To better explain why the open method isn't satisfactory, here's a sample bash session (with witty commentary).
% cd /my_repo
% gitk
Waiting for the GUI to appear ... any day now ... oh wait -- it's already open. I just didn't notice because it opened a window BEHIND my terminal. I wonder how long I was going to sit here waiting....
% open gitk
The file /my_repo/gitk does not exist.
Ah, of course.
% which gitk
/usr/bin/gitk
% open /usr/bin/gitk
What the ... it opened a new terminal window to run gitk, and it did so in my home directory, not /my_repo, so gitk complains that the current directory isn't actually a repository...
Do you need to invoke it synchronously? If not, you could start it asynchronously with & and then activate it with osascript -e 'tell application "gitk" to activate'.
If you are dealing with gitk specifically you can edit the gitk file to achieve this, I posted an answer on the apple stack exchange: https://apple.stackexchange.com/a/74917/35956
You can find the gitk file using the following command from the terminal
which gitk
In my gitk file I found a line that looks like the following near the top (line 3)
exec wish "$0" -- "$#"
I changed it to this
exec wish "$0" -- "$#" & exec osascript -e "tell application \"Wish\" to activate"
When I execute gitk from the command line the gitk window comes to the foreground, another side effect of this is that it executes asynchronously
You can wrap up #chris page's answer in a bash function and drop it in your .bashrc
function gitk() {
command gitk "$#"&
command osascript -e "delay .5" -e "tell application \"wish\" to activate"
}
There should be a way to get rid of the delay by looping and looking for 'wish' with a timeout.
NOTE: 'Wish' is the window title that shows up on my Mac for gitk.

Linux equivalent of the DOS "start" command?

I'm writing a ksh script and I have to run a executable at a separate Command Prompt window.
xdg-open is a similar command line app in linux.
see https://superuser.com/questions/38984/linux-equivalent-command-for-open-command-on-mac-windows for details on its use.
I believe you mean something like xterm -e your.sh &
Don't forget the final &
maybe it´s not a seperate window that gets started, but you can run some executables in background using "&"
e.g.
./myexecutable &
means your script will not wait until myexecutable has finished but goes on immediately. maybe this is what you are looking for.
regards
xdg-open is a good equivalent for the MS windows commandline start command:
xdg-open file
opens that file or url with its default application
xdg-open .
opens the currect folder in the default file manager
One of the most useful terminal session programs is screen.
screen -dmS title executable
You can list all your screen sessions by running
screen -ls
And you can connect to your created screen session (also allowing multiple simultaneous/synchronized sessions) by running
screen -x title
This will open up the emulated terminal in the current window where executable is running. You can detach a screen session by pressing C-a C-d, and can reattach as many times as you wish.
If you really want your program started in a new terminal window, you could do something like this:
xterm yourtextmodeprogram
or
gnome-terminal -e yourtextmodeprogram
or
konsole -e mc
Trouble is that you cannot count on a particular terminal emulator being installed, so (again: if you really want to do this) you would need to look for the common ones and then execute the first one encountered.
As Joachim mentioned: The normal way to do this is to background the command (read about shell job control somewhere, if you want to dig deeper).
There are also cases where you want to start a persistent shell, i.e. a shell session which lives on when you close the terminal window. There are two ways to do this:
batch-oriented: nohup command-to-run &
interactive: screen
if you want a new windows, just start a new instance of your terminal application: in kde it's
konsole -e whatever
i'm sure the Gnome terminal has similar options
Some have recommended starting it in the background with &, but beware that that will still send all console output from the application you launch to the terminal you launched it from. Additionally, if you close the initial terminal the program you loaded will end.
If you're using a desktop environment like KDE or GNOME, I'd check the alt+f2 launching apps (gnome-open is the one for GNOME, I don't know the name of the KDE app) and see if you can pass them the command to launch as an argument.
Also, if your intention is to launch a daemon, you should check the nohup documentation.
I used nohup as the following command and it works:
nohup <your command> &
then press enter and enter!
don't forget the last &
for example, I ran a python code listening to port 5000:
nohup python3 -W ignore mycode.py &
then I made sure of running by netstat -tulnp | grep :5000 and it was ok.

Resources