Run Jupyter Notebooks as a Service on macOS

I was looking for a way to run jupyer notebook without using a constantly open terminal or a screen session. On macOS there is a way to create services using a .plist file and the launchctl command.

Because I use different environments for all Python related stuff, it wasn’t immediately clear to me how to do this when I’ve installed Jupyter only in a pyenv. After some fiddling around, I finally solved it. The key was to set the working directory parameter to the directory in which the environment is loaded. In my case this is ~/notebooks where I automatically load the env by using a .python-version file.

Below you find the .plist file, that enables you to start/stop the notebook server using launchctl <start|stop> local.jupyter.notebook.

It assumes that you have a Python environenment in /Users/USERNAME/notebooks. So simply change it to your needs. Put the file into /Users/USERNAME/Library/LaunchAgents and call launchctl load PATH_TO_FILE once to load it. After this you can start/stop the service as described above.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Disabled</key>
    <false/>
    <key>KeepAlive</key>
    <false/>
    <key>Label</key>
    <string>local.jupyter.notebook</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/USERNAME/.pyenv/shims/jupyter</string>
        <string>notebook</string>
        <string>--notebook-dir</string>
        <string>/Users/USERNAME/notebooks</string>
        <string>--no-browser</string>
    </array>
    <key>StandardErrorPath</key>
    <string>/Users/USERNAME/Library/LaunchAgents/jupyter-notebook.stderr</string>
    <key>StandardOutPath</key>
    <string>/Users/USERNAME/Library/LaunchAgents/jupyter-notebook.stdout</string>
    <key>WorkingDirectory</key>
    <string>/Users/USERNAME/notebooks</string>
</dict>
</plist>

 

Leave a comment

avatar
  Subscribe  
Notify of