5

I'm using an aws ubuntu instance. I would like to create an alias/function to run some shortcuts, for example to activate a python virtual environnement in a screen.

I've made this function for example:

# Alias for jupyter notebook
function start_jupyter() {
    cd my_path/lab_workspace/  # 1. cd into my workspace
    source labworkspaceenv/bin/activate  # 2. activate my python virtualenv
    screen -S jupyter_lab  # 3. start screen
    echo 'You are in screen for jupyter lab'  # 4. print something
    jupyter lab  # 5. start jupyter lab
}

Problem is, when I run the function with start_jupyter, this seems to stop after 3. the screen is created but then nothing is printed and jupyterlab is not started.

What am I doing wrong?

1 Answer 1

6

The reason the function stopped is that you spawn an interactive screen session. You might want to do this instead:

screen -dmS jupyter_lab jupyter lab

This will create a detached screen session named jupyter_lab and execute the command there.

As per the screen --help info:

-dmS name     Start as daemon: Screen session in detached mode.

So, your function would be:

# Alias for jupyter notebook
function start_jupyter() {
    cd my_path/lab_workspace/  # 1. cd into my workspace
    source labworkspaceenv/bin/activate  # 2. activate my python virtualenv
    screen -dmS jupyter_lab jupyter lab  # 3. start screen    
}
3
  • Thanks for the answer and the working solution! So this is not possible to run screen in attached mode and execute code from the function ? Let's say for example I want to start a screen with my python env and then use it directly from the screen session
    – Yohann L.
    Jul 20, 2021 at 12:35
  • 1
    @YohannL. You can't do it non-interactively. Because you execute a script in a shell, then you spawn a screen session (attached) you lost focus from the previous shell. But it still carry the environment variables from the spawning shell.
    – annahri
    Jul 20, 2021 at 14:30
  • Okay thanks a lot for the clarification !
    – Yohann L.
    Jul 20, 2021 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .