6

I have a notebook that automatically queries a database, and then pushes the results to a google sheet every hour, but needs to be constantly running on a computer to work. How can I automatically run a Jupiter notebook when my computer starts up (I reboot a lot) Thanks!

Johnny
  • 8,939
  • 2
  • 28
  • 33
Matthew P
  • 61
  • 1
  • 1
  • 3

4 Answers4

6

I didn't actually tried with Anaconda but the solution should be comparable. The idea is to create vb script and run it from the scheduled task.

  1. Create jupyter-cron.vbs

    Set run = WScript.CreateObject("WScript.Shell")
    run.Run "jupyter-notebook.exe --no-browser --notebook-dir=C:\Notebooks", 0, True
    

    Note: Use --no-browser option to prevent browser to start. The script will run with the hidden window. Do not forget to update path to the notebook directory.

  2. Create jupyter-cron.bat

    start /B "C:\Windows\SysWOW64\cscript.exe" "C:\Localdata\jupyter-cron.vbs"
    

    Note: This location applies for 64-bit system, for the 32-bit system use C:\Windows\System32\cscript.exe. Do not forget to update path to vbs script.

  3. Define scheduled task to run on system startup Trigger Action

Johnny
  • 8,939
  • 2
  • 28
  • 33
2

Here is how I got it working using Johnny's steps as a template. (I would have responded with a comment to his post but I don't have enough rep.)

  1. Created jupyter.vbs file like Johnny mentioned. Replace $pathToAnaconda with your path and $pathToJupyterDirectory with where you want your jupyter to open to.
Set WinScriptHost = WScript.CreateObject("WScript.Shell")
WinScriptHost.Run "C:\{$pathToAnaconda}\Anaconda3\Scripts\activate.bat & jupyter notebook --no-browser --notebook-dir=C:\{$pathToJupyterDirectory}", 0, True
Set WinScriptHost = Nothing
  1. Created jupyter.bat file like Johnny mentioned. Replace $pathToVbsScript with path to your vbs file you made in step 1.
start /B "C:\Windows\SysWOW64\cscript.exe" "C:\{$pathToVbsScript}\jupyter.vbs"
  1. Go to the Windows Start Menu and type Task Scheduler. Open it, click New Task, then continue with Johnny's step #3.
Nate
  • 131
  • 2
  • 7
1

This is what I do for this sort of thing making use of Windows Task Scheduler. The con to this method is your computer has to be on and connected to the internet, but if you're working anyway this sort of doesn't matter.

Save your notebook as a .py file to your local (C:/users/username) directory then create a batch file using Notepad with the following script:

@echo on 
call c:\PROGRA~1\Anaconda3\Scripts\activate.bat activate YOURPYTHONENVIRONMENT
call C:\Users\username\.conda\envs\YOURPYTHONENVIRONMENT\python.exe "C:\Users\username\pythonfile.py"
pause

save the notepad file with the name 'filename.bat' with filename being replaced with whatever you want. Save the bat file wherever, I save by bat files on my work drive.

Then open windows task schedule and create a basic task

  1. Give it a name
  2. Set the schedule
  3. Select "Start a Program" as the action you want to perform
  4. Copy and paste the path to the bat file in the program/script bar

Then finish and you're done, the program will execute automatically without opening jupyter on your schedule.

Alex Dowd
  • 39
  • 9
0

I suggest you to use papermill library. It allows you to run a Jupyter notebook with command line. And you don't need instance of Jupyter running in the background. check this answer: how to run a python jupyter notebook daily automatically

Then, schedule a hourly task with Task Scheduler in Windows, which will be executed when your computed will be switched on.

mbh86
  • 6,078
  • 3
  • 18
  • 31