7

What would be the best "standard" way to create a launchd script to run python notebook?

I would like the service to start at login and an easy to start-stop-restart the server from a terminal.

7

In the end I created the following .plist file. It is autoloaded on startup and it will start a jupiter notebook and keep it alive.

<?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>KeepAlive</key>
          <true/>
          <key>Label</key>
          <string>cern.chernals.ipython</string>
          <key>ProgramArguments</key>
          <array>
              <string>/usr/local/bin/jupyter-notebook</string>
          </array>
          <key>RunAtLoad</key>
          <true/>
          <key>StandardErrorPath</key>
          <string>/Users/chernals/Library/LaunchAgents/jupyter-notebook.stderr</string>
          <key>StandardOutPath</key>
          <string>/Users/chernals/Library/LaunchAgents/jupyter-notebook.stdout</string>
              <key>KeepAlive</key>
              <true/>
      </dict>
      </plist>
  • 2
    I've written a script that configures a launchagent like this automatically. Check it out here. – nathanielobrown Jul 8 '17 at 21:41
1

I'm a macOS newbie, and found the accepted answer very helpful. I modified it to not launch a browser at startup and also start in the directory ~/jupyter where I store most of my notebooks.

For future reference, the way to make this start is to put the xml data in a file in the directory ~/Library/LaunchAgents, e.g. ~/Library/LaunchAgents/org.jupyter.notebook.plist.

The plist I ended up with was the following.

<?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>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>org.jupyter.notebook</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/jupyter-notebook</string>
        <string>--no-browser</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/Users/rickard/Library/LaunchAgents/org.jupyter.notebook.stderr</string>
    <key>StandardOutPath</key>
    <string>/Users/rickard/Library/LaunchAgents/org.jupyter.notebook.stdout</string>
    <key>KeepAlive</key>
    <true/>
    <key>WorkingDirectory</key>
    <string>/Users/rickard/jupyter</string>
</dict>
</plist>

I haven't figured out a way to not have to hard code the user's home directory in there -- the EnableGlobbing option only works in ProgramArgument, and there is no environment variable expansion, so for now it needs to be in there.

You must log in to answer this question.

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