How to run Jupyter Lab on every startup on amazon ec2 instance? - amazon-web-services

I have already tried user-data method and rc.local methods but none are working. I am not a pro so would like some help on this.
These are the 3 commands i want to run on every startup of ec2 instance:
tmux (start a tmux session so i dont loose the data when connection resets)
source pyenv/bin/activate (Activate the venv)
jupyter-lab --ip 0.0.0.0 --no-browser --allow-root (run the jupyter lab)
I'm using a ubuntu ec2 instance btw. Thanks in advance.
If i can acheieve this using nohup instead of tmux i'd be willing to that as well.
I wasn't able to find a solution anywhere so any help is appreciated, thank you.

Related

How to run bash script on Google Cloud VM?

I found this auto shutdown script for VM instances on GCP and tried to add that into the VM's metadata.
Here's a link to that shutdown script.
The config sets it so that after 20 mins the idle VM will shut down, but it's been a few hours and it never shut down. Are there any more steps I have to do after adding the script to the VM metadata?
The metadata script I added:
Startup scripts are executed while the VM starts. If you execute your "shutdown script" at the boot there will be nothing for it to do. Additionally in order for this to work a proper service has to be created and it will be using this script to detect & shutdown the VM in case it's idling.
So - even if the main script ashutdown was executed at boot and there was no idling it did nothing. And since the service wasn't there to run it again your instance will run indefinatelly.
For this to work you need to install everything on the VM in question;
Download all three files to some directory in your vm, for example with curl:
curl -LJO https://raw.githubusercontent.com/GoogleCloudPlatform/ai-platform-samples/master/notebooks/tools/auto-shutdown/ashutdown
curl -LJO https://raw.githubusercontent.com/GoogleCloudPlatform/ai-platform-samples/master/notebooks/tools/auto-shutdown/ashutdown.service
curl -LJO https://raw.githubusercontent.com/GoogleCloudPlatform/ai-platform-samples/master/notebooks/tools/auto-shutdown/install.sh
Make install.sh exacutable: sudo chmod +x install.sh
Run it: sudo ./install.sh.
This should install & run the ashutdown service in your system.
You can check if it's running with service ashutdown status.
These instructions are for Debian system so if you're running CentOS or other flavour of Linux they may differ.

Google Cloud Shell Keep Disconnecting

looking for solution to prevent google cloud shell disconnecting when it found you idle, even it also disconnect when you run some processing and leave system idle.
message shown Connection to Cloud Shell has been lost. Any additional changes will not be saved.
This behavior is by design, as Cloud Shell is intended for interactive use only.
One way to fix it is running a ping command within a TMUX Terminal.
apt install tmux
apt update
tmux
ping google.com
Ctrl+b "
The downside of this is you'll be working halfscreen.

Vertex AI notebook kernel lost on PC sleep

While using Vertex AI notebook instance kernel on GCP, the notebook gets detached everytime my system sleeps.
How can I keep my notebook running even if my system shuts down?
The Jupyter community has discussed this issue for quite a while now. There is no fix as such but there is a workaround to buffer the output and then display it when the notebook is opened again.
This answer is adopted from the comment from this Stack thread. I’ve also seen this workaround being suggested in a Jupyter Github issue.
The workaround would be to install the “Screen” utility (terminal multiplexer) on the GCE instance where JupyterHub is hosted, launch a new terminal session from JupyterHub and execute the notebook using the below “nbconvert” command.
jupyter nbconvert --to notebook --inplace --execute /home/path/to/notebook.ipynb
This way the terminal session could be preserved even if the personal computer is shut down and allow it to be resumed with the screen -r command.

Restart Jupyter Lab server running in the background

I'm trying to restart a Jupyter Lab server (not just the kernels) running in the background of an AWS SageMaker notebook instance. I have already tried the following:
Killing the server by it's process ID
pgrep doesn't show me the process
pkill can't find the process
ps aux shows the process ID as constantly changing
Stopping the server through jupyter notebook stop
I get an SSL error and nothing happens
The only thing I've been able to do is reboot the entire instance, which isn't a great option as it can take awhile to become available again.
Edit 1:
The main reason I am trying to do this is that after installing the tqdm package and trying to use tqdm.notebook in Jupyter Lab, in order for it to display correctly I need to enable/install notebook and lab extensions. In order for these to take effect the server then needs to be restarted.
Try this:
Left hand navbar, Commands
Navigate to the Help section on the popout menu
Reset Application State
Both classic Jupyter and Jupyter lab live within the same process.
sudo initctl restart jupyter-server --no-wait is what AWS suggest in https://forums.aws.amazon.com/thread.jspa?messageID=917594&#917594
Assuming it runs on port 8888:
jupyter lab stop 8888 && jupyter lab

Jupyter notebook kernel disconnecting / dead after sometime and cannot reconnect to amazon EC2 instance and complete ( currently running) operation.

When I connect to EC2 instance via Mobaxterm, after some period of time my jupyter notebook's kernel loses connection.
And some highly time-consuming operations /(Currently running tasks) are required to be re-performed again and again and are never-ending (This repeats each and every time).
I'm closing the notebook and restarting, so I can gain a connection to the kernel because it doesn't reconnect and I had to go through the process again and again when it dies eventually.
It also shows SSL error, wrong version number sometimes before disconnecting.
I have also faced a similar problem. I solved it with the help of 'tmux'.
I followed these steps:
I installed 'tmux' in my machine in the AWS instance.
[Actually, it came preinstalled with the AMI I had been using on the EC2 instance.]
I created a 'tmux' session simply by entering the command: tmux
Then I ran necessary commands to run the Jupyter server or Jupyter notebook
To close the terminal, I used this command: (i) ctrl + b, (ii) d
[Please notice, the session will continue running on the EC2 instance until you close the instance or close the jupyter server or the jupyter notebook].
To connect to the session again, I used the command: tmux attach
To finally kill the 'tmux' session when I am done, I used the command: tmux kill-session
Just use nohup. This should be the builtin tool in all Linux machines.
So you should do: nohup jupyter notebook > output.txt
And then you can safely terminate the console session without worrying about killing the notebook.