Run a Job

Scenario Overview

This guide shows how to initiate CNC server jobs for tasks like scheduling, retry failures, or trigger one-time operations.

Prerequisites

Before running, obtain an access token ('Getting Started'), configure cnc-api-common.sh, and ensure that input/output directories exist.

Retrieve Devices with a Specific Collection Status

Run run_job.sh from the example directory, specifying the job name and type as jobName:jobType.

cd cnc-inventory-api-examples;./run_job.sh

Script Details

#!/bin/bash
. ./cnc-api-common.sh
run_job() {
    # Assuming the CNC JWT has already been obtained by running the get-cnc-jwt.sh script.
    # Read from JWT file and export it as AUTH_TOKEN_HDR
     export_jwt

    # STEP 1  :  Retrieve all the job specifications on the server
    # STEP 2  :  At the input prompt, enter the job name and job type (in the format jobName:jobType) for the job you want to run.
    # STEP 3  :  Trigger runJob API.
    # STEP 4  :  Wait for 30 seconds, retrieve the updated job specifications, and copy them to a file.
    
    echo -e "\n\nGet details of all the jobs on the server: \n"
    retrieve_job_specifications 

    echo -e "\nPlease enter the job you want to run in the format jobName:jobType (Ex. Switch Inventory:Inventory):"
    read job_name
    CNC_API_INPUT="$PRJ/input/run_job.json"  
    echo "$job_name" > $CNC_API_INPUT
    
    CNC_API_URL=$CNC_INVENTORY_JOB_API_CTX/runJob
    CNC_API_OUTPUT="$PRJ/output/run_job.json"
    http_post $CNC_API_URL @$CNC_API_INPUT $CNC_API_OUTPUT
    
    echo -e "\nWaiting 30sec.."
    sleep 30
    retrieve_job_specifications
    echo -e "\n\nUpdated Job Specifications copied to $JOB_SPECS_OUTPUT\n" 
}
run_job