Introduction
JS7 offers a number of JS7 - Job Types including JS7 - Java Jobs, JS7 - JavaScript Jobs and Shell Jobs.
...
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:
Code Block |
---|
language | js |
---|
title | Example how to invoke a Shell to run script code |
---|
linenumbers | true |
---|
|
#!/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:
Code Block |
---|
language | bash |
---|
title | Example how run PowerShell® script code with a shebang |
---|
linenumbers | true |
---|
|
#!/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:
Code Block |
---|
language | bash |
---|
title | Example how to run PowerShell® script code from a file |
---|
linenumbers | true |
---|
|
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:
Code Block |
---|
language | bash |
---|
title | Example how use Python script code with a shebang |
---|
linenumbers | true |
---|
|
#!/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:
Code Block |
---|
language | bash |
---|
title | Example how to run Python script code from a file |
---|
linenumbers | true |
---|
|
#!/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:
Code Block |
---|
language | js |
---|
title | Example how run Node.js® script code with a shebang |
---|
linenumbers | true |
---|
|
#!/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:
Code Block |
---|
language | js |
---|
title | Example how to run Node.js® script from a file |
---|
linenumbers | true |
---|
|
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:
Code Block |
---|
language | bash |
---|
title | Example how run Shell script code |
---|
linenumbers | true |
---|
|
@echo off
echo hello world
hostname |
This includes to call .bat and .cmd command files like this:
Code Block |
---|
language | bash |
---|
title | Example how run Shell script code |
---|
linenumbers | true |
---|
|
@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:
Code Block |
---|
language | bash |
---|
title | Example how run PowerShell® script code with a shebang replacement |
---|
linenumbers | true |
---|
|
@@findstr/v "^@@f.*&" "%~f0"|powershell.exe -&goto:eof
Write-Output "Hello"
Write-Output "world" |
Note: PowerShell 5.1 frequently ships with the 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:
Code Block |
---|
language | bash |
---|
title | Example how to run PowerShell® script code from a file |
---|
linenumbers | true |
---|
|
powershell.exe -NoLogo -NonInteractive -File some_powershell_script.ps1 |
Running Python Scripts
Python can be invoked to execute script code like this:
Code Block |
---|
language | bash |
---|
title | Example how use Python script code with a shebang |
---|
linenumbers | true |
---|
|
@@findstr/v "^@@f.*&" "%~f0"|python.exe -&goto:eof
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:
Code Block |
---|
language | bash |
---|
title | Example how to run Python® script code from a single line |
---|
linenumbers | true |
---|
|
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:
Code Block |
---|
language | js |
---|
title | Example how run Node.js® script code with a shebang replacement |
---|
linenumbers | true |
---|
|
@@findstr/v "^@@f.*&" "%~f0"|node.exe -&goto:eof
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:
Code Block |
---|
language | js |
---|
title | Example how to run Node.js® script from a file |
---|
linenumbers | true |
---|
|
node.exe C:\Users\Documents\sample_Node.js |
Accessing Arguments
Passing Variables
Further Resources
How To
Links
Display content by label |
---|
Labels | js7 job shell howto |
---|
OtherTitle | How To ... Shell Jobs |
---|
|
Display content by label |
---|
Labels | js7 shell api howto |
---|
OtherTitle | How To ... Shell API |
---|
|