Exit ipython console without killing the kernel - ipython

I'm connecting an IPython console to an existing kernel, via
ipython console --existing /path/to/your/kernel.json
However, when I exit the console, this kills the kernel:
In [8]: exit
Shutting down kernel
(same thing happens with ctrl+D, or any other method that I would normally use to get out of an IPython session). My intention is to have the ipython console temporarily as a debug tool for an IPython notebook. Obviously, I don't want the notebook kernel to die when I finish debugging. I don't want to use qtconsole, as all of this is running on a remote server, to which I'm connected via SSH/tmux. I can't easily "just close" the IPython console. I could supposedly kill the tmux pane, but that's rather cumbersome and just feels wrong. Any better options?

The console can be quit without killing the kernel: quit(keep_kernel=True)

Related

How to run initialization commands after SSH in VS Code Remote?

Problem
I am trying to connect to my school's computing cluster (aka a linux server with "login node" and "computing node") using VS Code's Remote SSH, but I cannot figure out how to run a command after SSH-ing.
Goal
I simply want to view Python code and test some small lines in a .ipynb jupyter notebook in the computing platform's environment.
Description
Basically, normally in the command line (or mobaXterm of a Windows machine) of my local machine, I first log onto the computing platform's login node with ssh -Y -L PORT:127.0.0.1:PORT username#computing.cluster.ip, and then run srun -t 0-12:00 --pty -p gpu --gres=gpu:1 --x11 --tunnel PORT:PORT /bin/bash to log onto the computing node interactively (shown command allows for port forwarding). The problem is, in VS Code I can only connect to the login node, but after that there's no way for me to run another command and log onto the computing node. The reason I need to get to computing node is that I want to test something with a .ipynb file interactively on VS Code while reading the code, and the login node does not allow me to perform computation.
Failed trials
I've been trying Code-Server, but it does not support .ipynb well (it keeps asking me to install jupyter notebook even though I have installed it in my conda env), possibly because it by default recognizes HPC cluster's Python interpreter which I cannot modify (I can't even select Jupyter kernel in code-server). I also tried to directly use Jupyter Notebook (open Jupyter with port forwarding after getting onto computing node), but reading code on it is much more inconvenient.
Would greatly appreciate your suggestions!

Juypter notebook - connect to the same kernel session from a command line ipython?

Suppose I've a Jupyter notebook loaded in a browser. Is that possible to somehow run a command line ipython session which connect to the same kernel insance as the one used by the notebook (i.e they can see the same set of variables)? Thanks.
Yes! After starting/loading an IPython notebook, open up a terminal and connect a command line ipython session using the --existing parameter:
jupyter console --existing
By default, it will connect to the latest started IPython kernel. To select a different kernel to connect to, look in the log of the Jupyter notebook for lines like these:
[I 09:47:54.462 NotebookApp] Kernel started: 06c9ffae-ae9f-4c22-93c0-4eacf23672b1
To connect to this kernel, do:
jupyter console --existing 06c9ffae-ae9f-4c22-93c0-4eacf23672b1
With JupyterLab it's possible to open a linked console by right clicking on any Notebook and selecting New Console for Notebook (as mentioned in #machine_building's comment on this answer). That will show a connected console beside/below the Notebook, which one can interact with like in a terminal with command history etc.

IPython notebook: Browser crashed, is it possible to reconnect to running session/kernel

I have a notebook running for some hours, I came back in the morning and at some point the browser crashed. I can see that the kernel is still running and computing.
If I open the browser and open the notebook again, it seems as it opens the file, but does not really connect to the existing running kernel. I judge this by the fact that the notebook does not show the "kernel busy" badge on the top.
What I really would like to do is, is to connect to the running kernel with a qtconsole, interrupt the process, go into debug mode and save the important variables, shut down the kernel properly and continue the process with the data I saved.
Now is there a way of connecting to a running kernel with a qtconsole?
For future reference:
Alternatively one could also wait until the process finished the computation and then hook into the session and save the results (but this works only if the kernel is idle, hence the question is still open):
Just copy the hash from the terminal that started the ipyhton notebook and then link into the session with:
ipython console --existing 08906ba1-7942-4309-9955-712efdeb09f9
or
ipython qtconsole --existing 08906ba1-7942-4309-9955-712efdeb09f9
(08906ba1-7942-4309-9955-712efdeb09f9 being the hash)

emacs as a screen/tmux alternative - detach from terminal

It's well known that emacs can be used as a terminal emulator (while itself is running in a terminal emulator), thus making it a valid alternative to more traditional terminal-in-a-terminal approaches, such as tmux or screen. However, there's one thing that could be done easily with the latter and I've found no alternative in emacs' term for this one so far.
Both tmux and screen can detach from a terminal and all tasks ran in their windows continue to run in background. It's done using C-b, d in tmux and C-a, d in screen by default. Later, I can return (reattach) to the terminal I've detached from by running something like tmux attach or screen -r. Also, sessions run in both of these terminal multiplexers are persistent - i.e. if I'm connected to some remote terminal and connection fails, I can reconnect and reattach to the terminal without losing any of my work - it really helps in case of faulty network link that occasionally breaks ssh connections.
Is there something like that available for emacs? Basically, I'd want to be able to:
Detach from emacs and leave it running in background with all the sub-processes ran in term buffers intact.
Reattach to it later and find all my processes running.
Automatic detachment of emacs from terminal on receiving a SIGHUP.
Use emacs daemon:
$ emacs --daemon
Then simply launch a new frame, equivalent for screen -x:
$ emacsclient -t

How to make a server daemon which re-runs automatically when they're terminated unexpectedly?

I'm trying to running OrientDB on Ubuntu. Currently, I'm running with bin/server.sh. This works fine except it runs foreground on shell. I can make it work background by Ctrl+Z and bg command, but this doesn't mean it's running as daemon.
I wish the program will keep running after I logout. And will be started again when it terminated unexpectedly or OS restarts. Like MS Windows Services. But the problem is I don't know how can I do this.
How can I run a program as a long-running service?
If you do not own the server, look into using the "screen" command. It will allow you to run a command, detach from the console where the command is running, then log out while leaving it running. You may reconnect to the running screen to see output or restart the script. Here's more info about the screen command:
http://www.manpagez.com/man/1/screen/
If you own the server, you should write an init script. It's not very hard, and you can set it up to run automatically on startup. The system will run the script with a "start" parameter when you want it to start, and a "stop" parameter when you want it to stop. Here's more detailed information:
http://www.novell.com/coolsolutions/feature/15380.html
If the command doesn't already detach from the console (run in daemon mode) then in the init script place the command in parenthesis to run in it's own shell. You will not see any output unless you pipe it to a file within the parenthesis.
(bin/server.sh >> /var/log/server.log)