Unix background processes stop running - unix

From my Windows 7 box with Cygwin, I ssh into a Solaris box.
Windows box:
# uname -a
CYGWIN_NT-6.1 KSTLINTC20V1335 1.7.9(0.237/5/3) 2011-03-29 10:10 i686 Cygwin
Solaris box:
$uname -a
SunOS dncs 5.10 Generic_147441-01 i86pc i386 i86pc
Suppose I log into the Solaris box from the Windows box:
ssh -Y user#host
I make sure my DISPLAY is set, and I open a xterm:
/usr/openwin/bin/xterm
It open on my display.
Now, I suspend it with CTRL-z. Then I type bg to put it into the background. I can't log out of the first window until that xterm process is finished.
So I tried to run it as such
nohup /usr/openwin/bin/xterm &
The xterm runs in the background as expected, but when I try to disconnect from my ssh session in the original window, it tells me I have running jobs. I thought nohup disconnected the process from the terminal.
And if I close the original window, then my xterm goes away too.
I also tried this, from the windows box:
ssh -Y user#host /usr/openwin/bin/xterm
The xterm opens as expected. However, if I suspend it and put it into the background: CTRL-z and bg, the xterm will not respond. I cannot type anything into it until I bring it into the foreground with fg.
Why is this happening? What do I need to do "revive" my xterms (and other X window processes) which I put them into the background?
I can run the following, and it works as expected:
nohup ssh -Y user#host /usr/openwin/bin/xterm &

Related

How to use xterm to launch screen with arguments

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.

Can tmux query physical terminal? (Maybe iTerm2 only)

I'm trying to detect the presence of iTerm2 and if I run the following in iTerm2 (echo -n $'\e[5n'; read -s -t 0.1 line; printf '%q\n' "$line") the terminal responds with $'\033'\[ITERM2\ 3.2.1n$'\033'\[0n
However, if I am running a tmux session in the terminal, then tmux responds and gives me nothing.
Any idea how I can ask tmux to query the physical terminal to report its status?
Footnotes
Here is a description of [5n in the tmux source: https://github.com/tmux/tmux/blob/486ce9b09855ae30a2bf5e576cb6f7ad37792699/tools/ansicode.txt#L577
This might be iTerm2 only, since I haven't seen a response on any other terminal
According to ft in freenode's #tmux (and as seen in this Super User answer), you can use:
'\ePtmux;\e" STUFF_FOR_THE_TERMINAL_HERE "\e\\'
So, it would be something like:
echo -n $'\ePtmux;\e\e[5n\e\\'

How to run jupyter notebook in the background ? No need to keep one terminal for it

Often we run jupyter notebook to pop up a page in browser to use notebook. However, the terminal opening the server remains there. Is there a way that we can close that terminal with server running in the back ?
You can put the process into the background by using jupyter notebook --no-browser & disown. You can close the terminal afterwards and the process will still be running.
If you're using zsh you can also use a shorter version that does the same: jupyter notebook --no-browser &!.
To kill the process you can use pgrep jupyter to find the PID of the process and then kill 1234, replacing 1234 with the PID you just found.
Explanation
The --no-browser flag makes jupyter not open the browser automatically, it also works without this flag.
The & puts it into the background of the currently running shell.
The disown then removes the job from the background of the currently running shell and makes it run independently of the shell so that you may close it.
In the zsh version the &! is a built-in function that does the same as & disown.
Tmux is a good option available to run your Jupyter Notebook in the background.
I am running my Jupyter Notebook on a google cloud platform's VM instance with OS: Ubuntu 16.0. Where
I have to start SSH terminal
Then run the command: jupyter-notebook --no-browser --port=5000. It will start the Jupyter Notebook on port number 5000 on that VM instance
Then I open my browser and typer ip_addrees_of_my_instance:port_number which is 5000. It will open my Notebook in the browser.
Now up to this, all is good. But wait if the connection with my SSH terminal is terminated then the Jupyter Notebook stops immediately and hence I have to re-run it once again once the ssh terminal is restarted or from new ssh terminal.
To avoid this tmux is very good option.
Terminal Multiplexer (tmux) to Keep SSH Sessions Running in the background after ssh terminal is closed:
Start the ssh terminal
Type tmux. It will open a window in the same terminal.
Give command to start Jupyter Notebook here. Open Notebook.
Now if SSH terminal is closed/terminated it will keep running your notebook on the instance.
If the connection terminated then:
reconnect or open new ssh terminal. To see this Jupyter Server(which is kept running in the background) type: tmux attach command.
(Edited: changed "notebook" to "Jupyter Server")
Want to terminate tmux session:
Close the notebook. Then type exit in tmux-terminal-window.
(update: If we close the notebook and use tmux detach command: it will exit from tmux session window/terminal without terminating/stopping the tmux sessions)
For more details please refer to this article: https://www.tecmint.com/keep-remote-ssh-sessions-running-after-disconnection/
Under *nix, the best way to run a program avoiding to be terminated by closing the terminal is to use nohup (no Hang up).
To start browser after running the server use the command:
nohup jupyter notebook &
And to start the server without opening the browser use the command:
nohup jupyter notebook --no-browser &
Note that you can shut down the jupyter server by using Quit in the upper right of the page of jupyter.
nohup puts as a parent of the process init(0), so it will not receive the "Hang Up" signal when the terminal is closed. All the output (standard output and standard error) are redirected to the file nohup.out
nohup exists both as program and shell command, so if you have bash, check man page of bash to read more details.
This works for me when running a jupyter notebook server in the background.
$> nohup jupyter notebook --allow-root > error.log &
Stop the nohup jupyter notebook is simple.
First, find the pid of jupyter:
$> ps -ef| grep jupyter
e.g output like:
root 11417 2897 2 16:00 pts/0 00:04:29 /path/to/jupyter-notebook
Then kill the process:
$> kill -9 11417
You can also simplify this by storing the pid with:
$> nohup jupyter notebook --allow-root > error.log & echo $!> pid.txt
i.e, you can stop the notebook with:
$> kill -9 $(cat pid.txt)
An alternative way to stop the jupyter notebook is quit from the notebook page.
You can use screen to run it.
screen -A -m -d -S anyscreenname jupyter notebook --no-browser
This will start jupyter in a screen and you can access screen using screen commands.
Actually, jupyter notebook & alone is not enough, the backend will still log to your screen.
What you need is, cited from this issue
jupyter notebook > /path/to/somefileforlogging 2>&1 &
You can start up the notebook in a screen or tmux session. Makes it easy to check error messages, etc.
For remote machines jupyter notebook & works fine.
However, it does not work on local machines when you close the terminal.
For local machines use tmux.
Not real sophisticated but it gets the job done:
#! /bin/bash
#probably should change this to a case switch
if [ "$1" == "end" ]
then
echo
echo "Shutting Down jupyter-notebook"
killall jupyter-notebook
echo
exit 0
fi
if [ "$1" == "-h" ]
then
echo
echo "To start : jnote <port> [default 8888]"
echo "To end : jnote end"
echo "This help : jnote -h"
echo
exit 0
fi
#cast from string
PORT=$(($1))
RETURN=0
PID=0
if [ "$PORT" == "0" ] || [ "$PORT" == "" ]; then PORT=8888; fi
echo
echo "Starting jupyter-notebook"
#background and headless, set port, allow colab access, capture log, don't open browser yet
nohup jupyter notebook \
--NotebookApp.allow_origin='https://colab.research.google.com' \
--port=$PORT --NotebookApp.port_retries=0 \
--no-browser >~/jnote.log 2>&1 &
RETURN=$?
PID=$!
#Wait for bg process to complete - add as needed
sleep 2
if [ $RETURN == 0 ]
then
echo
echo "Jupyter started on port $PORT and pid $PID"
echo "Goto `cat ~/jnote.log | grep localhost: | grep -v NotebookApp`"
echo
exit 0
else
echo
echo "Jupyter failed to start on port $PORT and pid $PID with return code $RETURN"
echo "see ~/jnote.log"
echo
exit $RETURN
fi
jupyter notebook & >> disown
put the process into the background by using jupyter notebook &
then type disown or disown <the process id>
you can close the terminal now
Detach Jupyter process from the controlling terminal and send all its input and output data to /dev/null which is a special device file that writes-off any data written to it.
jupyter notebook </dev/null &>/dev/null &
Lazy people like me would prefer to edit ~/.bash_aliases and create an alias:
alias jnote='jupyter notebook </dev/null &>/dev/null &'
Reference: https://www.tecmint.com/run-linux-command-process-in-background-detach-process/
As suggested by one of the users, using
jupyter notebook &
solves the issue. Regarding the comments stating that it kills the kernel after closing the terminal, probably you are using
jupyter-notebook &.
If you are using iTerm2 software,
first you need to set:
brew shellenv
Then start jupyter in nohup:
eval $(/usr/local/bin/brew shellenv)
nohup jupyter notebook &
Here is a command that launches jupyter in background (&) detached from the terminal process (disown) and without opening the browser (--no-browser)
No log will be shown on the terminal since they are redirected (&>) to a file jupyter_server.log so they can still be referred to later.
jupyter notebook --no-browser &> jupyter_server.log & disown
If you don't wan't to store the logs(discouraged):
jupyter notebook --no-browser &> /dev/null & disown
Thanks to the other answers this one is built upon: here and there

R interactive plots over SSH often freeze

Main question
I do a lot of interactive work in R using SSH and tmux. I connect using the -Y flag, e.g.
ssh -Y user#server
Then, I typically start a new tmux session:
tmux new -s name
When I first connect, my plots work fine. However, my connection often gets interrupted or I might disconnect for lunch. For this case, I have a line in my .bashrc that saves my current display:
if [ ! -f ~/.my.display ]; then
touch ~/.my.display
fi
display=$(echo $DISPLAY)
if [[ $display != "" ]]; then
echo "export DISPLAY="$DISPLAY > ~/.my.display
fi
source ~/.my.display
After disconnecting, when I tmux into my interactive R session, I typically need to reset my X11 window:
# reconnect to tmux session
ssh -Y user#server
tmux a -t name
# reset x11 window
graphics.off()
display = gsub(".*DISPLAY.", "", tail(readLines("~/.my.display"), n = 1))
x11(display, type = "Xlib")
This works most of the time. However, around 25% of the time, my graphics.off() call hangs for 30 minutes, which is incredibly frustrating. I often need to kill my session, losing all of my work.
Is there a way to call graphics.off() without it hanging for so long? Ideally, it would try this command for like 10 seconds, then throw an error if there is a problem.
Bonus
Within tmux, I often connect to other user accounts using SSH -Y. Therefore, the sequence of commands is:
# connect laptop -> user1
ssh -Y user1#server1
tmux a -t name
# connect user1 -> user2
ssh -Y user2#server2
I am having trouble making interactive plots with user2's account. Right now, my solution is to save user2's DISPLAY variable to their .my.display. Before plotting with user2 through tmux, I first SSH directly into user2's account, which properly properly configures their .my.display file. Then, I follow the same steps as before: graphics.off() and starting a new x11 window.
This seems unnecessarily complicated, so my second question is if there is a cleaner way to set this up?
I am using XQuartz 2.7.11 on Mac OS 10.12.6

xterm with screen and trap command

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?

Resources