Tutorial - Creating your first job flow

This example shows how to write command and script jobs that run in sequence. 

Before you begin

Ensure that you have set up your environment, as described in Setting up the prerequisites.

Step 1 - Access the tutorial samples

Go to the directory where the tutorial sample is located:

cd automation-api-quickstart/101-create-first-job-flow

Step 2 - Verify the code for Control-M

Let's take the AutomationAPISampleFlow.json file, which contains job definitions, and verify that the code within it is valid. To do so, use the build command.  The following example shows the command and a typical successful response.

> ctm build AutomationAPISampleFlow.json

[
  {
    "deploymentFile": "AutomationAPISampleFlow.json",
    "successfulFoldersCount": 0,
    "successfulSmartFoldersCount": 1,
    "successfulSubFoldersCount": 0,
    "successfulJobsCount": 2,
    "successfulConnectionProfilesCount": 0
  }
]

If the code is not valid, an error is returned.

Step 3 - Run the source code

Use the run command to run the jobs on the Control-M environment. The returned runId is used to check the job status. The following shows the command and a typical successful response.

> ctm run AutomationAPISampleFlow.json

{
  "runId": "7cba67de-9e0d-409d-8d93-1b8229432eee",
  "statusURI": "https://localhost:8443/automation-api/run/status/7cba67de-9e0d-409d-8d93-1b82294e?token=4f8684ec6754e08cc70f95b5f09d3a47_A1FD0E65",
  "monitorPageURI": "https://localhost:8443/SelfService#Workbench:runid=7cba67de-9e0d-409d-8d93-29432eee&title=AutomationAPISampleFlow.json"
}

This code ran successfully and returned the runId of "7cba67de-9e0d-409d-8d93-1b8229432eee".

Step 4 - Check job status using the runId

The following command shows how to check job status using the runId. Note that when there is more than one job in the flow, the status of each job is checked and returned.

> ctm run status "7cba67de-9e0d-409d-8d93-1b8229432eee"

{
  "statuses": [
    {
      "jobId": "workbench:00007",
      "folderId": "workbench:00000",
      "numberOfRuns": 1,
      "name": "AutomationAPISampleFlow",
      "type": "Folder",
      "status": "Executing",
      "startTime": "Apr 26, 2017 10:43:47 AM",
      "endTime": "",
      "outputURI": "Folder has no output",
      "logURI": "https://localhost:8443/automation-api/run/job/workbench:00007/log?token=01ab65917bc71dbef610806dd9cb3f94_0007C46B"
    },
    {
      "jobId": "workbench:00008",
      "folderId": "workbench:00007",
      "numberOfRuns": 0,
      "name": "CommandJob",
      "folder": "AutomationAPISampleFlow",
      "type": "Command",
      "status": "Wait Host",
      "startTime": "",
      "endTime": "",
      "outputURI": "Job did not run, it has no output",
      "logURI": "https://localhost:8443/automation-api/run/job/workbench:00008/log?token=01ab65917bc71dbef610806dd9cb3f94_0007C46B"
    },
    {
      "jobId": "workbench:00009",
      "folderId": "workbench:00007",
      "numberOfRuns": 0,
      "name": "ScriptJob",
      "folder": "AutomationAPISampleFlow",
      "type": "Job",
      "status": "Wait Condition",
      "startTime": "",
      "endTime": "",
      "outputURI": "Job did not run, it has no output",
      "logURI": "https://localhost:8443/automation-api/run/job/workbench:00009/log?token=01ab65917bc71dbef610806dd9cb3f94_0007C46B"
    }
  ],
  "startIndex": 0,
  "itemsPerPage": 25,
  "total": 3,
  "monitorPageURI": "https://localhost:8443/SelfService#Workbench:runid=7cba67de-9e0d-409d-8d93-1b8229432eee&title=Status_7cba67de-9e0d-409d-8d93-1b8229432eee"

Step 5 - Examine the source code

Let's look at the source code in the AutomationAPISampleFlow.json file. By examining the contents of this file, you'll learn about the structure of the job flow and what it should contain.

{
    "Defaults" : {
        "Application" : "SampleApp",
        "SubApplication" : "SampleSubApp",
        "RunAs" : "<USERNAME>",
        "Host" : "<HOST>",
        "Job": {
            "When" : {
                "Months": ["JAN", "OCT", "DEC"],
                "MonthDays":["22","1","11"],
                "WeekDays":["MON","TUE", "WED", "THU", "FRI"],
                "FromTime":"0300",
                "ToTime":"2100"
            },
            "ActionIfFailure" : {
                "Type": "If",
                "CompletionStatus": "NOTOK",
                "mailToTeam": {
                    "Type": "Mail",
                    "Message": "%%JOBNAME failed",
                    "To": "team@mycomp.com"
                }
            }
        }
    },
    "AutomationAPISampleFlow": {
        "Type": "Folder",
        "Comment" : "Code reviewed by John",
        "CommandJob": {
            "Type": "Job:Command",
            "Command": "echo my 1st job"
        },
        "ScriptJob": {
            "Type": "Job:Script",
                "FilePath":"<SCRIPT_PATH>",
                "FileName":"<SCRIPT_NAME>"
        },
        "Flow": {
            "Type": "Flow",
            "Sequence": ["CommandJob", "ScriptJob"]
        }
    }
}

The first object is called "Defaults". It allows you to define a parameter once for all objects. For example, it includes scheduling using the When parameter, which configures all jobs to run according to the same scheduling criteria. The "ActionIfFailure" object determines what action is taken if a job ends unsuccessfully.

This example contains two jobs: CommandJob and ScriptJob. These jobs are contained within a folder named AutomationAPISampleFlow. To define the sequence of job execution, the Flow object is used.

Step 6 - Modify the code to run in your environment

In the code above, the following parameters need to be set to run the jobs in your environment: 

"RunAs" : "<USERNAME>" 
"Host" : "<HOST>"
 
"FilePath":"<SCRIPT_PATH>"
"FileName":"<SCRIPT_NAME>"

RunAs identifies the operating system user that will execute the jobs.

Host defines the machine where the jobs will run. This machine should have a Control-M/Agent installed. 

FilePath and FileName define the location and name of the file that contains the script to run on the Control-M/Agent.

Change the values to suit your Control-M environment. In a Control-M Workbench environment, change the values of the following parameters as shown in the example below. The ag_diag_comm script returns a communication report. 

"RunAs" : "workbench" 
"Host" : "workbench"
 
"FilePath":"/home/workbench/ctm/scripts"
"FileName":"ag_diag_comm"

Step 7 - Rerun the code sample

Now that we've modified the source code in the AutomationAPISampleFlow.json file, let's rerun the sample:

> ctm run AutomationAPISampleFlow.json

{
  "runId": "ed40f73e-fb7a-4f07-a71c-bc2dfbc48494",
  "statusURI": "https://localhost:8443/automation-api/run/status/ed40f73e-fb7a-4f07-a71c-bc2dfbc48494?token=460e0106b369a0d155bb0e7cbb44f8eb_7E6C03FA",
  "monitorPageURI": "https://localhost:8443/SelfService#Workbench:runid=ed40f73e-fb7a-4f07-a71c-bc2dfbc48494&title=AutomationAPISampleFlow.json"
}

Each time you run the code, a new runId is generated. Let's take the new runId, and check the jobs statuses again:

> ctm run status "ed40f73e-fb7a-4f07-a71c-bc2dfbc48494"

{
  "statuses": [
    {
      "jobId": "workbench:0000p",
      "folderId": "workbench:00000",
      "numberOfRuns": 1,
      "name": "AutomationAPISampleFlow",
      "type": "Folder",
      "status": "Ended OK",
      "startTime": "May 3, 2017 4:57:25 PM",
      "endTime": "May 3, 2017 4:57:28 PM",
      "outputURI": "Folder has no output",
      "logURI": "https://localhost:8443/automation-api/run/job/workbench:0000p/log?token=a8d74f5914dc6decdfd8b2ec833d54cc_3E30FFC9"
    },
    {
      "jobId": "workbench:0000q",
      "folderId": "workbench:0000p",
      "numberOfRuns": 1,
      "name": "CommandJob",
      "folder": "AutomationAPISampleFlow",
      "type": "Command",
      "status": "Ended OK",
      "startTime": "May 3, 2017 4:57:26 PM",
      "endTime": "May 3, 2017 4:57:26 PM",
      "outputURI": "https://localhost:8443/automation-api/run/job/workbench:0000q/output?token=a8d74f5914dc6decdfd8b2ec833d54cc_3E30FFC9",
      "logURI": "https://localhost:8443/automation-api/run/job/workbench:0000q/log?token=a8d74f5914dc6decdfd8b2ec833d54cc_3E30FFC9"
    },
    {
      "jobId": "workbench:0000r",
      "folderId": "workbench:0000p",
      "numberOfRuns": 1,
      "name": "ScriptJob",
      "folder": "AutomationAPISampleFlow",
      "type": "Job",
      "status": "Ended OK",
      "startTime": "May 3, 2017 4:57:27 PM",
      "endTime": "May 3, 2017 4:57:27 PM",
      "outputURI": "https://localhost:8443/automation-api/run/job/workbench:0000r/output?token=a8d74f5914dc6decdfd8b2ec833d54cc_3E30FFC9",
      "logURI": "https://localhost:8443/automation-api/run/job/workbench:0000r/log?token=a8d74f5914dc6decdfd8b2ec833d54cc_3E30FFC9"
    }
  ],
  "startIndex": 0,
  "itemsPerPage": 25,
  "total": 3,
  "monitorPageURI": "https://localhost:8443/SelfService#Workbench:runid=ed40f73e-fb7a-4f07-a71c-bc2dfbc48494&title=Status_ed40f73e-fb7a-4f07-a71c-bc2dfbc48494"
}

You can now see that both jobs Ended OK.

Let's view the output of CommandJob. Use the jobId to get this information.

> ctm run job:output::get "workbench:0000q"

+ echo my 1st job
my 1st job

Step 8 - View job details through an interactive interface

Control-M Workbench offers an interactive user interface for debugging purposes. Through this interface, you can view various job run details (including, for example, an activity log and statistics for each job). To launch this interface when you run jobs, enter "--interactive" or "-i" at the end of the run command.

> ctm run AutomationAPISampleFlow.json --interactive

{
  "runId": "40586805-60b5-4acb-9f21-a0cf048f1051",
  "statusURI": "https://ec2-54-187-1-168.us-west-2.compute.amazonaws.com:8443/run/status/40586805-60b5-4acb-9f21-a0cf048f1051",
  "monitorPageURI": "https://localhost:8443/SelfService#Workbench:runid=40586805-60b5-4acb-9f21-a0cf048f1051&title=AutomationAPISampleFlow.json
}

A browser window opens, where you can view and manage your jobs.

Where to go from here

Was this page helpful? Yes No Submitting... Thank you

Comments

  1. Adriano Gomes

    Hi

    IN The Step 5 - Examine the source code

    Let's look at the source code in the AutomationAPISampleFlow.json file. By examining the contents of this file, you'll learn about the structure of the job flow and what it should contain.

    There is an instruction missing peace of code on sample provided on the screen:

    You need to Add to "Defaults" like this below, otherwise the code will fail. "ControlmServer" : "workbench or ",

    Also, for Step 6 - Modify the code to run in your environment

    replace the "ControlmServer" : "workbench or ",

    My Best

    A>Gomes

    { "Defaults" : { "ControlmServer" : "workbench or ", "Application" : "SampleApp", "SubApplication" : "SampleSubApp", "RunAs" : "", "Host" : "", "Job": { "When" : { "Months": ["JAN", "OCT", "DEC"], "MonthDays":["22","1","11"], "WeekDays":["MON","TUE", "WED", "THU", "FRI"], "FromTime":"0300", "ToTime":"2100" }, "ActionIfFailure" : { "Type": "If", "CompletionStatus": "NOTOK", "mailToTeam": { "Type": "Mail", "Message": "%%JOBNAME failed", "To": "team@mycomp.com" } } } }, "AutomationAPISampleFlow": { "Type": "Folder", "Comment" : "Code reviewed by John", "CommandJob": { "Type": "Job:Command", "Command": "echo my 1st job" }, "ScriptJob": { "Type": "Job:Script", "FilePath":"", "FileName":"" }, "Flow": { "Type": "Flow", "Sequence": ["CommandJob", "ScriptJob"] } } }

    Jun 14, 2019 10:50
    1. Yechezkel Schatz

      Hi Adriano. Thanks for the feedback and sorry for taking so long. I double-checked with the Dev team and they told me this: When the Workbench is used, it is the only Control-M Server and is used by default, so you don't have to explicitly specify it.

      Jul 02, 2019 04:19