Cluster Node Initialization Scripts

An init script is a shell script that runs during startup for each cluster node before the Spark driver or worker JVM starts. Some examples of tasks performed by init scripts include:


To install Python packages, use the Azure Databricks pip binary located at /databricks/python/bin/pip to ensure that Python packages install into Databricks Python virtual environment rather than the system Python environment. For example, /databricks/python/bin/pip install <packagename>.

  • Modify the JVM system classpath in special cases.
  • Set system properties and environment variables used by the JVM.
  • Modify Spark configuration parameters.

Init script types

Azure Databricks supports three kinds of init scripts: global, cluster-named, and cluster-scoped. The order of execution of init scripts is:

  1. Global - runs on every cluster
  2. Cluster-named - runs on a cluster with the same name as the script
  3. Cluster-scoped - runs on every cluster that references the script

Global init scripts

Global init scripts are stored in dbfs:/databricks/init/ and run on every cluster.


Any change to a global init script requires a cluster restart.

To delete a global init script, delete the init script file. You can perform this either in a notebook or using the DBFS API or DBFS CLI. For example:


If you have created a global init script that is preventing new clusters from starting up, use the API or CLI to move or delete the script.

Cluster-named init scripts


  • Cluster-named init scripts are deprecated. Azure Databricks recommends that you use cluster-scoped init scripts.
  • You cannot use cluster-named init scripts for clusters that run jobs because job cluster names are generated on the fly. However, you can use cluster-scoped init scripts for job clusters.
  • Avoid spaces in cluster names since they’re used in the script and output paths.

Cluster-named scripts scope to a single cluster, specified by the cluster’s name. Cluster-named init scripts are stored in the directory dbfs:/databricks/init/<cluster-name>. For example, to specify init scripts for the cluster named PostgreSQL, create the directory dbfs:/databricks/init/PostgreSQL, and put all scripts that should run on cluster PostgreSQL in that directory.


Any change to a cluster-named init script requires a cluster restart.

To delete a cluster-named init script, delete the init script file. You can perform this either in a notebook or using the DBFS API or DBFS CLI. For example:


Cluster-scoped init scripts

Cluster-scoped init scripts are init scripts defined in a cluster configuration. Cluster-scoped init scripts apply to both clusters you create and those created to run jobs. Since the scripts are part of the cluster configuration, cluster access control lets you control who can change the scripts.

You can configure cluster-scoped init scripts using the UI, the CLI, and by invoking the Clusters API. This section focuses on performing these tasks using the UI. For the other methods, see Databricks CLI and Clusters API.

In the cluster configuration page, you configure the init script location at the bottom in the Init Scripts tab. You can add any number of scripts and the scripts are executed sequentially in the order provided.


You can put init scripts in any DBFS path accessible by the cluster. To configure an init script:

  1. In the Destination drop-down, select a destination type.
  2. Specify a path to the init script.
  3. Click Add.
  4. Upload your script to the specified location.


If the script pointed to by the configuration doesn’t exist, the cluster will fail to be created or autoscaled up.

To remove a script from the cluster configuration, click the Delete Script Icon at the right of the script. When you confirm the delete you will be prompted to restart the cluster. Optionally you can delete the script file from the location you uploaded it to.

Environment variables

Init scripts support the following environment variables:

  • DB_CLUSTER_ID- the ID of the cluster on which the script is running. See Clusters API.
  • DB_CONTAINER_IP- the private IP address of the container in which Spark runs. The init script is run inside this container. See SparkNode.
  • DB_IS_DRIVER - whether the script is running on a driver node.
  • DB_DRIVER_IP - the IP address of the driver node.
  • DB_INSTANCE_TYPE - the instance type of the host VM.
  • DB_PYTHON_VERSION - the version of Python used on the cluster. See Python Clusters.
  • DB_IS_JOB_CLUSTER - whether the cluster was created to run a job. See NewCluster.

For example, if you want to run a script only on a driver node, you could write a script like:

if [[ $DB_IS_DRIVER = "TRUE" ]]; then
  <run this part only on driver>
  <run this part only on workers>
<run this part on both driver and workers>

Cluster-scoped init script events

Init scripts report start and finish events in the cluster event log. Therefore, you can compute the time it takes for an init script to run by subtracting the start from the finish event timestamps.

Init script logs

Init scripts write logs that can be used to debug issues with the scripts.

Global and cluster-named init scripts

Databricks saves all init script output for global and cluster-named init scripts to a file in DBFS named as follows: dbfs:/databricks/init/output/<cluster-name>/<date-timestamp>/<script-name>_<node-ip>.log. For example, if a cluster PostgreSQL has two Spark nodes with IPs and, and the init script directory has a script called, there will be two output files at the following paths:

  • dbfs:/databricks/init/output/PostgreSQL/2016-01-01_12-00-00/installpostgres.sh_10.0.0.1.log
  • dbfs:/databricks/init/output/PostgreSQL/2016-01-01_12-00-00/installpostgres.sh_10.0.0.2.log

Cluster-scoped init scripts

By default, init script logs are stored in /databricks/init_scripts.

If cluster log delivery is configured, logs are delivered to that location. For each container, they will appear in a subdirectory called init_scripts/<cluster_id>_<container_ip>. For example, if cluster logs are delivered to dbfs:/cluster-logs, the directory would be: dbfs:/cluster-logs/init_scripts/<cluster_id>_<container_ip>. For example:

dbfs ls dbfs:/cluster-logs/1001-234039-abcde739/init_scripts

If the logs are delivered to DBFS you can view the logs using File system utilities. Otherwise, you can use the following code in a notebook to view the logs:

%sh ls /databricks/init_scripts/


Create a global init script


Global init scripts run on every cluster at cluster startup. Be careful about what you place in these init scripts.

  1. Create dbfs:/databricks/init/ if it doesn’t exist.

  2. Display the list of existing global init scripts.

  3. Create a script that simply appends to a file.

    dbutils.fs.put("dbfs:/databricks/init/" ,"""
    echo "hello" >> /hello.txt
    """, True)
  4. Check that the script exists.


Every time a cluster launches it runs this append script.

Create a cluster-scoped init script

This example creates an init script that installs a PostgreSQL JDBC driver on a cluster with ID 1202-211320-brick1.

  1. Create the base directory you want to store the init script in if it does not exist. Here we use dbfs:/databricks/<directory> as an example.

  2. Create the script.

    wget --quiet -O /mnt/driver-daemon/jars/postgresql-42.2.2.jar
    wget --quiet -O /mnt/jars/driver-daemon/postgresql-42.2.2.jar""", True)
  3. Check that the script exists.

  4. Configure the cluster to run the script.

    curl -n -X POST -H 'Content-Type: application/json' -d '{
      "cluster_id": "1202-211320-brick1",
      "cluster_log_conf": {
        "dbfs" : {
          "destination": "dbfs:/cluster-logs"
      "init_scripts": [ {
        "dbfs": {
          "destination": "dbfs:/databricks/<directory>/"
      } ]
    }' https://<databricks-instance>/api/2.0/clusters/edit

Create a cluster-named init script

This example creates an init script for a cluster named PostgreSQL that installs the PostgreSQL JDBC driver on that cluster. You can create a customizable command if you create a variable clusterName that holds the cluster name.

  1. Create dbfs:/databricks/init/ if it doesn’t exist.

  2. Display the list of existing global init scripts.

  3. Configure a cluster name variable.

    clusterName = "PostgreSQL"
  4. Create a directory named PostgreSQL using Databricks File System - DBFS.

  5. Create the script.

    wget --quiet -O /mnt/driver-daemon/jars/postgresql-42.2.2.jar
    wget --quiet -O /mnt/jars/driver-daemon/postgresql-42.2.2.jar""", True)
  6. Check that the cluster-specific init script exists.