hanspetter
New Contributor III

Hi! It's been a long time since I have looked at this problem, but I revisited it this week and found a solution. 🙂

This returns a json containing information about the notebook:

dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson() 

If the notebook has been triggered by dbutils.notebook.run, we can find the tag "jobId" here. So we can return the jobid using dbutils.notebook.exit(job_id):

import json 
notebook_info = json.loads(dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())
try: 
    #The tag jobId does not exists when the notebook is not triggered by dbutils.notebook.run(...) 
    jobId = notebook_info["tags"]["jobId"] 
except: 
    jobId = -1 
dbutils.notebook.exit(jobId) 

When executing the notebook we get:

jobid = dbutils.notebook.run(...) 
print(jobid) 

This outputs:

Notebook job #1522478

1522478

Well, this only works when a notebook successfully completes. What we have done is that we are inserting the jobid and notebook path together with the current timestamp to a db-table and query the table when an exception occurs to get the jobid.

View solution in original post