Introduction

JS7 offers a number of JS7 - Job Classes including JS7 - Java JobsJS7 - JavaScript Jobs and Shell Jobs.

  • Shell Jobs are started from an OS process by the JS7 Agent
  • Any number of instances of a Shell Job can be executed in parallel.

JS7 - Job Environment Variables are available to Shell Jobs.

Using Script Languages

Shell jobs execute job scripts in any scripting language available from the OS.

Unix

Running Shell Scripts

Shell scripts can be written in any Shell available from the OS such as bash, ksh, zsh, dash etc. It is recommended to add a shebang to the first line of the script that indicates the Shell to be used:

Example how to invoke a Shell to run script code
#!/bin/bash

# alternative use for bash
#!/usr/bin/env bash

# frequently used shebangs
#!/bin/sh
#!/bin/ksh
#!/bin/zsh
#!/bin/dash

Running PowerShell Scripts

In order to directly run PowerShell® script code from a JS7 shell job script the recommended approach is to use a shebang like this:

Example how run PowerShell® script code with a shebang
#!/usr/bin/env pwsh
 
Write-Output "Hello"
Write-Output "world"


In addition, a PowerShell® script can be executed from a file that is in reach of the JS( Agent:

Example how to run PowerShell® script code from a file
pwsh -NoLogo -NonInteractive -File some_powershell_script.ps1

Running Python Scripts

In order to directly run Python® script code from a JS7 shell job script the recommended approach is to use a shebang replacement like this:

Example how use Python script code with a shebang
#!/usr/bin/python
 
print("Hello")
print("world")


Alternatively a Python® script can be executed from a file that has to be located in reach of the JS7 Agent:

Example how to run Python script code from a file
#!/usr/bin/bash

python hello.py

Running Node.js JavaScript

In order to directly run Node.js® script code from a JS7 shell job script, the recommended approach is to use a shebang that runs Node.js® as the interpreter of the script like this:

Example how run Node.js® script code with a shebang
#!/usr/bin/node

var name = (process.env.name);
var num = parseInt(process.env.num);

console.log(name);
console.log(num);


Alternatively, a Node.js® script can be executed from a file that is located within reach of the JS7 Agent that runs the job:

Example how to run Node.js® script from a file
node  /some/location/sample_Node.js

Windows

Running Shell Scripts

Any commands available from the Windows Shell can be used in a job script like this:

Example how run Shell script code
@echo off

echo hello world
hostname


This includes to call .bat and .cmd command files like this:

Example how run Shell script code
@echo off

call C:\Documents\hello.bat
call C:\Documents\world.cmd

Running PowerShell Scripts

In order to directly run PowerShell® script code from a JS7 shell job script the recommended approach is to use a shebang replacement like this:

Example how run PowerShell® script code with a shebang replacement
@@setlocal enabledelayedexpansion & @@findstr/v \"^@@[fs].*&\" \"%~f0\" | powershell.exe -NonInteractive -Command - & exit !errorlevel!/b&

Write-Output "Hello" 
Write-Output "world" 


Note: PowerShell 5.1 frequently ships with the Windows OS and makes use of powershell.exe. Later PowerShell releases use the pwsh.exe binary.

In addition, a PowerShell® script can be executed from a file that is located in reach of the JS7 Agent:

Example how to run PowerShell® script code from a file
powershell.exe -NoLogo -NonInteractive -File some_powershell_script.ps1

Running Python Scripts

Python can be invoked to execute script code like this:

Example how use Python script code with a shebang
@@setlocal enabledelayedexpansion & @@findstr/v \"^@@[fs].*&\" \"%~f0\" | python.exe - & exit !errorlevel!/b&

print("Hello")
print("world")


Alternatively a Python® script can be executed from a file that has to be located in reach of the JS7 Agent:

Example how to run Python® script code from a single line
python.exe hello.py

Running Node.js JavaScript

In order to directly run Node.js® script code from a JS7 shell job script, the recommended approach is to use a shebang that runs Node.js® as the interpreter of the script like this:

Example how run Node.js® script code with a shebang replacement
@@setlocal enabledelayedexpansion & @@findstr/v \"^@@[fs].*&\" \"%~f0\" | node.exe - & exit !errorlevel!/b&

var name = (process.env.name);
var num = parseInt(process.env.num);

console.log(name);
console.log(num);


Explanation
:

  • If you consider this shebang replacement somewhat cryptic then add it to JS7 - Script Includes which are easily referenced from shell jobs, e.g. by using
    ::!include <name-of-script-include>
  • The node.exe executable as available from the Node.js® installation is executed by the shebang.

In addition, a Node.js® script can be executed from a file located with the JS7 Agent that implements the job:

Example how to run Node.js® script from a file
node.exe C:\Users\Documents\sample_Node.js

Accessing Arguments

Shell jobs access arguments from OS environment variables.

  • Such variables are declared with the workflow.
  • Shell jobs map workflow variables to environment variables for the job.
  • Shell job scripts make use of environment variables.

Declaring Workflow Variables

Variables are declared by a workflow like this:

  • Variables are specified by a name and data type. Optionally a default value is specified:
    • Orders are forced to specify values for workflow variables that are not assigned a default value.
    • Orders are free to overrule default values of given workflow variables.

Mapping Workflow Variables to Job Environment Variables

From the declared workflow variables jobs choose which variables to map to environment variables like this:

Using Environment Variables in Jobs

Jobs make use of environment variables in the job script. Depending on the OS in use the following syntax is used:

  • Unix: ${<variable-name>} or $<variable-name>
    • Names of variables are case-sensitive.
  • Windows: %<variable-name>%
    • Names of variables are case-insensitive.


Examples:

Example for use of Environment Variables in a Unix Shell job script
#!/bin/bash

echo "using workflow: $JS7_WORKFLOW_NAME"
echo "running job: $JS7_JOB_NAME"

echo "Business Date: $BUSINESS_DATE"
echo "Flight Number: $FLIGHT_NUMBER"
Example for use of Environment Variables in a Windows Shell job script
echo using workflow: %JS7_WORKFLOW_NAME%
echo running job: %JS7_JOB_NAME%

echo Business Date: %BUSINESS_DATE%
echo Flight Number: %FLIGHT_NUMBER%
Example for use of Environment Variables in a PowerShell job script
#!/usr/bin/env pwsh

echo "using workflow: $env:JS7_WORKFLOW_NAME"
echo "running job: $env:JS7_JOB_NAME"

echo "Business Date: $env:BUSINESS_DATE"
echo "Flight Number: $env:FLIGHT_NUMBER"
Example for use of Environment Variables in a Python job script
#!/usr/bin/python

import os

print('using workflow: ', os.environ.get('JS7_WORKFLOW_NAME'))
print('running job: ', os.environ.get('JS7_JOB_NAME'))

print('Business Date: ', os.environ.get('BUSINESS_DATE'))
print('Flight Number: ', os.environ.get('FLIGHT_NUMBER'))
Example for use of Environment Variables in a Node.js job script
#!/usr/bin/node

console.log('using workflow: ' + process.env.JS7_WORKFLOW_NAME);
console.log('running job: ' + process.env.JS7_JOB_NAME);

console.log('Business Date: ' + process.env.BUSINESS_DATE);
console.log('Flight Number: ' + process.env.FLIGHT_NUMBER);

Passing Variables

Jobs can pass variables to subsequent jobs and JS7 - Workflow Instructions like this:

  • The JS7 Agent offers an environment variable JS7_RETURN_VALUES that holds the path to a temporary file.
  • Jobs can append key/value pairs to the temporary file.
  • On completion of the job the JS7 Agent will read the temporary file and will make related workflow variables available for subsequent jobs.

Examples:

Example for passing variables from a Unix Shell job script
#!/bin/bash

SOME_VAR=$RANDOM
echo "my_var=$SOME_VAR" >> $JS7_RETURN_VALUES
Example for passing variables from a Windows Shell job script
set SOME_VAR=%RANDOM%
echo my_var=%SOME_VAR% >> %JS7_RETURN_VALUES%
Example for passing variables from a PowerShell job script
#!/usr/bin/env pwsh

$someVar = Get-Random
"my_var=$someVar" | Out-File $env:JS7_RETURN_VALUES -Append
Example for passing variables from a Python Shell job script
#!/usr/bin/python
 
import random;

someVar = random.randint(0,9)
with open(os.environ.get('JS7_RETURN_VALUES'), 'a') as f:
    print(f"my_var={someVar}", file=f)
Example for passing variables from a Node.js job script
#!/usr/bin/node

const fs = require('fs');
var someVar = Math.random();
fs.appendFile(process.env.JS7_RETURN_VALUES, 'my_var=' + someVar + '\n', (err) => {});

Further Resources

How To

How To ... Shell Jobs

How To ... Shell API



  • No labels