This topic describes how to create script jobs by writing Shell, Python, or PHP scripts.
Introduction
The following figure shows how to use script jobs.

To use script jobs, deploy
schedulerx-agenton the target device in advance to communicate with the server, receive script execution commands, and return execution results.Deploy the dependencies required for running scripts on the device in advance, such as Python dependency libraries and dependent files.
Each time a script is executed,
schedulerx-agentforksa child process to run the script. This child process does not occupy the memory ofschedulerx-agent, but it occupies the memory of the device (the specific memory consumption depends on the script content). If a large number of script jobs are running simultaneously, the device may run out of memory. We recommend that you use Kubernetes jobs to run scripts through pods in scenarios in which a large amount of computing resources are required to run scripts.
Prerequisites
The SchedulerX agent is deployed. For more information, see Use the SchedulerX agent to connect an application to SchedulerX (Script or HTTP jobs).
Create a script job
Log on to the Distributed Task Scheduling Platform, click Task Management in the left-side navigation pane, and create a job on the Task Management page. When you create a job, select ScriptTask for Task type.

Step 1: Select a script language
Select shell, python, or php from the Script drop-down list as the script language.
Step 2: Select an execute command
Commands for executing the script in different languages:
Shell script: The default command is
/bin/sh.Python script: The default command is
python. You can change the command topython2orpython3.PHP script: The default command is
php.
Step 3: Select a file format
The line feed varies when you select unix or windows for File format.
To execute the script on a
Windowssystem, selectwindows.To execute the script on
Linux, Unix, or macOS, selectunix.
When the script source is on a Windows system but needs to run on a Linux system, select unix for File format.
Step 4: Select an execution mode
Access prerequisite: schedulerxAgent is deployed in advance to run scripts. For more information, see Use the SchedulerX agent to connect an application to SchedulerX (Script or HTTP jobs).
Execution mode:
Stand-alone operation: If you deploy multiple SchedulerX agents and set Execution mode to Stand-alone operation, SchedulerX randomly selects a SchedulerX agent to run the script.
Broadcast run: If you deploy multiple SchedulerX agents and set Execution mode to Broadcast run, SchedulerX broadcasts the script to all the SchedulerX agents. This way, the script runs on all SchedulerX agents at the same time. This setting is preferred for batch O&M.
Shard run: See Create a Python sharding job.
Write scripts
After a script job is created, a script template is generated by default based on the script language. You can modify the script content by clicking EditScript on the Task Management page.

Historical script versions
Professional Edition applications can use the historical script version feature. On the EditScript page, turn on the Historical versions switch to go to the version comparison page.
The left side shows the current version, and the right side shows the historical version.
Click the dropdown box to view historical version information, including version notes and creation time.
After you modify the script content, click Confirm. You will be prompted to enter a new version name.

Demo
Distributed processing based on job broadcasting
Connect to two
schedulerx-agents.Create a new script job with Script set to shell and Execution mode set to Broadcast run. The following figure shows the script content.

Click Run once in the Operation column of the job.
Click Execution List in the left-side navigation pane. On the Execution List page, click Log in the Operation column of the job instance to view the content executed by each machine.
The following figure shows the running logs of
172.16.13.125. According to the logs, the shard ordinal number obtained is 1, and the total number of shards is 2.
The following figure shows the running logs of
172.16.13.120. According to the logs, the shard ordinal number obtained is 0, and the total number of shards is 2.
Distributed processing based on job sharding
Create a new script job with Script set to shell and Execution mode set to Shard run. The following figure shows other job configurations.

After you create the job, click EditScript for the job. The following figure shows the script content.

Click Run once in the Operation column of the job.
Click Execution List in the left-side navigation pane. On the Execution List page, click Log in the Operation column of the job instance to view the sharding parameters of each machine.
According to the logs, two shards (
hangzhouandbeijing) are allocated to and run on172.16.13.120.Two shards (
shenzhenandshanghai) are allocated to and run on172.16.13.125.

References
For system built-in parameters, see System variables of scripts.