1e41f4b71Sopenharmony_ci# Job Management
2e41f4b71Sopenharmony_ci## Overview
3e41f4b71Sopenharmony_ci### Function
4e41f4b71Sopenharmony_ciA job is a set of commands in the **.cfg** file of the init module. A maximum of 4096 jobs can be added. Jobs can be configured in the [.cfg file](subsys-boot-init-cfg.md). Generally, jobs are executed during initialization to serve the normal startup of services or the initialization of specific basic functions.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci### Basic Concepts
7e41f4b71Sopenharmony_ciA job can be configured in the **init.cfg** file or the custom **.cfg** file of the module. The parser of the init process aggregates commands of the jobs with the same name into one job. For jobs with the same name, the init process only ensures that the commands in the **init.cfg** file are executed in preference. It does not guarantee the execution sequence of commands in other **.cfg** files.
8e41f4b71Sopenharmony_ci- Basic job
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci  A job executed in a fixed phase during init process startup, for example, pre-init, init, or post-init.
11e41f4b71Sopenharmony_ci  - pre-init: pre-initialization phase. Key services on which other services depend, such as ueventd, watchdog, and hilogd, are started in this phase. The mounting of data partitions is also done in this phase.
12e41f4b71Sopenharmony_ci  - init: main phase of the initialization process. In this phase, a large number of commands are executed, and services in the **boot** group (first group) are started concurrently by the **init** group. Some important services related to system functions are also started in this phase.
13e41f4b71Sopenharmony_ci  - post-init: post-initialization phase. In this phase, the **trigger** command is used to invoke the execution of other phases, which can be regarded as independent phases, or the post-init phase as a whole. In this phase, a large number of commands are executed, and the **normal** group (the second group) is started by the **init** group. Most services configured in the **.cfg** files are started in this phase.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci- Custom job (for standard system or higher)
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci  A job triggered based on specific rules. You can add commands to the job as required and run the **trigger** command to invoke the execution of commands in the job.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci- Conditional job (for standard system or higher)
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci  A job triggered based on specific conditions. You can add conditions to a job so that the job is executed when the conditions are met.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci  A condition is a combination of system parameter values. It supports operations such as **&&** and **||** as well as matching of any parameter values by using the wildcard character (*).
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci  Generally, you can configure a condition in the format shown below:
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci  ```
28e41f4b71Sopenharmony_ci  "condition" : "sys.usb.config = none && sys.usb.configfs = 0",
29e41f4b71Sopenharmony_ci  ```
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci  If you need to enable parameter checking in the boot phase, configure the condition as follows:
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci  ```
34e41f4b71Sopenharmony_ci  "condition" : "boot && const.debuggable=1",
35e41f4b71Sopenharmony_ci  ```
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci  When defining commands for a job, you can add attributes in the format of **param:xxx** to form different commands.
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci### Constraints
40e41f4b71Sopenharmony_ciWith the system parameter module, the standard system is able to support basic, conditional, and custom jobs. The small system supports only basic jobs.
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci## How to Develop
43e41f4b71Sopenharmony_ci### Use Cases
44e41f4b71Sopenharmony_ciA job is a command set, where you can manage the commands to be executed. A maximum of 4096 commands can be added to a command set. During the init startup process, the execution of jobs helps ensure normal running of services.
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci### Parameter Description
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci   **Table 1** Command set description
49e41f4b71Sopenharmony_ci   | Command| Format and Example| Description|
50e41f4b71Sopenharmony_ci   | -------- | -------- | -------- |
51e41f4b71Sopenharmony_ci   | mkdir | mkdir <i>target folder</i> <i>[mode]</i> <i>[owner]</i> <i>[group]</i><br>Example:<br/>mkdir /storage/myDirectory<br>mkdir /storage/myDirectory 0755 root root| Creates a folder. <strong>mkdir</strong> and the target folder must be separated by only one space.<B><br>System type: small system and standard system|
52e41f4b71Sopenharmony_ci   | chmod | chmod <i>permission</i> <i>target</i><br>Example:<br/>chmod 0600 /storage/myFile.txt<br>chmod&nbsp;0750&nbsp;/storage/myDir | Modifies the permission, which must be in the <strong>0</strong><i>xxx</i> format. <strong>chmod</strong>, <i>permission</i>, and <i>target</i> must be separated by only one space.<B><br>System type: small system and standard system|
53e41f4b71Sopenharmony_ci   | chown | chown <i>uid</i> <i>gid</i> <i>target</i><br>Example:<br/>chown 900 800 /storage/myDir<br>chown&nbsp;100&nbsp;100&nbsp;/storage/myFile.txt | Modifies the owner group. <strong>chown</strong>, <strong>uid</strong>, <strong>gid</strong>, and <i>target</i> must be separated by only one space.<B><br>System type: small system and standard system|
54e41f4b71Sopenharmony_ci   | mount | mount&nbsp;fileSystemType&nbsp;src&nbsp;dst&nbsp;flags&nbsp;[data]<br>Example:<br/>mount vfat /dev/mmcblk0 /sdc rw,umask=000<br>mount&nbsp;jffs2&nbsp;/dev/mtdblock3&nbsp;/storage&nbsp;nosuid | Mounts devices. Every two parameters must be separated by only one space. For details about <strong>flags</strong>, see the <strong>mountFlagMap[]</strong> function in <strong>base/startup/init/services/init/init\_common\_cmds.c</strong>. The <strong>data</strong> field is optional.<B><br>System type: small system and standard system|
55e41f4b71Sopenharmony_ci   | start | start&nbsp;serviceName<br>Example: start foundation| Starts services. <i>serviceName</i> must be contained in the <strong>services</strong> array.<B><br>System type: small system and standard system|
56e41f4b71Sopenharmony_ci   | export | export key value<br>Example:<br>export TEST /data/test| Sets environment variables. <strong>key</strong> and <strong>value</strong> respectively indicate the environment variable and its value.<B><br>System type: small system and standard system|
57e41f4b71Sopenharmony_ci   | rm | rm filename<br>Example:<br>rm /data/testfile| Deletes a file. <i>filename</i> indicates the absolute file path.<B><br>System type: small system and standard system|
58e41f4b71Sopenharmony_ci   | rmdir | rmdir dirname<br>Example:<br>rmdir /data/testdir| Deletes a directory. <i>dirname</i> indicates the absolute path of the directory. <B><br>System type: small system and standard system|
59e41f4b71Sopenharmony_ci   | write | write filename value<br>Example:<br>write /data/testfile 0| Writes a file. <strong>filename</strong> and <strong>value</strong> respectively indicate the absolute file path and the string to write. <B><br>System type: small system and standard system|
60e41f4b71Sopenharmony_ci   | stop | stop serviceName<br>Example:<br>stop console| Stops a service. <i>servicenamei> indicates the name of the service to stop.<B><br>System type: small system and standard system|
61e41f4b71Sopenharmony_ci   | copy | copy oldfile newfile<br>Example:<br>copy /data/old /data/new| Copies a file. <i>oldfile</i> and <i>newfile</i> respectively indicate the old and new absolute file paths.<B><br>System type: small system and standard system|
62e41f4b71Sopenharmony_ci   | reset | reset serviceName<br>Example:<br>reset console| Resets a service. <i>servicename</i> indicates the name of the service to reset. If the service has not been started, this command will start the service. If the service is running, the command will stop the service and then restart it.<B><br>System type: small system and standard system|
63e41f4b71Sopenharmony_ci   | reboot | reboot [subsystem]<br>Example:<br>reboot updater| Restarts the system. <strong>subsystem</strong> is optional. If it is not specified, the device enters the current system upon restarting. If it is specified, the device enters the corresponding subsystem upon restarting. For example, if you run <strong>reboot updater</strong>, the device enters the updater subsystem upon restarting.<B><br>System type: small system and standard system|
64e41f4b71Sopenharmony_ci   | sleep | sleep time<br>Example:<br>sleep 5| Enters the sleep mode. <i>time</i> indicates the sleep time, which must not exceed 5 seconds.<br>To avoid impact on services, exercise caution when running this command.<B><br>System type: small system and standard system|
65e41f4b71Sopenharmony_ci   | domainname | domainname name<br>Example:<br>domainname localdomain| Sets the domain name.<B><br>System type: small system and standard system|
66e41f4b71Sopenharmony_ci   | hostname | hostname name<br>Example:<br>hostname localhost| Sets the host name.<B><br>System type: small system and standard system|
67e41f4b71Sopenharmony_ci   | wait | wait filepath [time]<br>Example:<br>wait /data/testfile or wait /data/testfile 5| Waits for commands. <i>time</i> indicates the waiting time, which must not exceed 5 seconds.<B><br>System type: small system and standard system|
68e41f4b71Sopenharmony_ci   | setrlimit | setrlimit resource curValue maxValue<br>Example:<br>setrlimit RLIMIT_CPU 10 100| Sets resource usage restrictions.<B><br>System type: small system and standard system|
69e41f4b71Sopenharmony_ci   | write | write path content<br>Example:<br>write /proc/sys/kernel/sysrq 0| Writes a file.<B><br>System type: small system and standard system|
70e41f4b71Sopenharmony_ci   | exec | exec <i>Path of the executable file</i> <i>Parameters passed by the executable file</i><br>Example:<br>exec /system/bin/mkdir /data/test.txt| Runs an executable file. This command is called by the system.<B><br>System type: small system and standard system|
71e41f4b71Sopenharmony_ci   | syncexec | syncexec <i>Path of the executable file</i> <i>Parameters passed by the executable file</i><br>Example:<br>syncexec /system/bin/udevadm trigger| Runs an executable file synchronously. The **wait** function will be called to wait for the child process to end. The command must not contain more than 10 parameters.<B><br>System type: standard system
72e41f4b71Sopenharmony_ci   | mknode |mknod name { b \| c } Major Minor<br>Example:<br>mknod path b 0644 1 9| Creates an index node corresponding to a directory entry and a special file.<B><br>System type: standard system|
73e41f4b71Sopenharmony_ci   | makedev | makedev major minor<br>Example:<br>makedev -v update| Creates a static device node, which is usually in the <strong>/dev</strong> directory.<B><br>System type: standard system|
74e41f4b71Sopenharmony_ci   | symlink | symlink target link_name<br>Example:<br>symlink /proc/self/fd/0 /dev/stdin| Creates a symbolic link.<B><br>System type: standard system|
75e41f4b71Sopenharmony_ci   | trigger | trigger jobName<br>Example:<br>trigger early-fs| Triggers a job.<B><br>System type: standard system|
76e41f4b71Sopenharmony_ci   | insmod | insmod [-f] [options]<br>Example:<br>insmod xxx.ko| Loads a kernel module file.<B><br>System type: standard system|
77e41f4b71Sopenharmony_ci   | setparam | setparam paramName paramValue<br>Example:<br>setparam sys.usb.config hdc| Sets system parameters.<B><br>System type: standard system|
78e41f4b71Sopenharmony_ci   | load_persist_params | load persist params<br>Example:<br>load_persist_params | Loads <strong>persist</strong> parameters. There must be one and only one space after the <strong>load_persist_params</strong> command.<B><br>System type: standard system|
79e41f4b71Sopenharmony_ci   | load_param | load params<br>Example:<br>load_param /data/test.normal.para| Loads the parameters from a file to the memory.<B><br>System type: standard system|
80e41f4b71Sopenharmony_ci   | load_access_token_id | load_access_token_id | Writes the access token to the <strong>data/service/el0/access_token/nativetoken.json</strong> file. There is one and only one space after <strong>load_access_token_id</strong>.<B><br>System type: standard system|
81e41f4b71Sopenharmony_ci   | ifup | ifup <i>NIC</i><br>Example:<br>ifup eth0| Activates the specified NIC.<B><br>System type: standard system|
82e41f4b71Sopenharmony_ci   | mount_fstab | mount_fstab fstab.test<br>Example:<br>mount_fstab /vendor/etc/fstab.test| Mounts partitions based on the <strong>fstab</strong> file.<B><br>System type: standard system|
83e41f4b71Sopenharmony_ci   | umount_fstab | umount_fstab  fstab.test<br>Example:<br>umount_fstab /vendor/etc/fstab.test| Unmounts partitions based on the <strong>fstab</strong> file.<B><br>System type: standard system|
84e41f4b71Sopenharmony_ci   | restorecon | restorecon file or dir<br>Example:<br>restorecon /file| Reloads the SELinux context.<B><br>System type: standard system|
85e41f4b71Sopenharmony_ci   | stopAllServices | stopAllServices [bool]<br>Example:<br>stopAllServices false or stopAllServices| Stops all services. The maximum response time is 10 ms by default.<B><br>System type: standard system|
86e41f4b71Sopenharmony_ci   | umount |umount path<br>Example:<br>umount /vendor| Unmounts a mounted device.<B><br>System type: standard system|
87e41f4b71Sopenharmony_ci   | sync | sync | Writes data to the disk synchronously. There is only one and only one space after <strong>sync</strong>.<B><br>System type: standard system|
88e41f4b71Sopenharmony_ci   | timer_start | timer_start serviceName<br>Example:<br>timer_start console| Starts the service timer.<B><br>System type: standard system|
89e41f4b71Sopenharmony_ci   | timer_stop | timer_stop serviceName<br>Example:<br>timer_stop console| Stops a service timer.<B><br>System type: standard system|
90e41f4b71Sopenharmony_ci   | init_global_key | init_global_key path<br>Example:<br>init_global_key /data| Initializes the encryption key of the data partition file.<B><br>System type: standard system|
91e41f4b71Sopenharmony_ci   | init_main_user | init_main_user| Encrypts the main user directory.<B><br>System type: standard system|
92e41f4b71Sopenharmony_ci   | mkswap | mkswap file<br>Example:<br>mkswap /swapfile1| Creates a swap partition on a file or device.<B><br>System type: standard system|
93e41f4b71Sopenharmony_ci   | swapon | swapon file <br>Example:<br>swapon /swapfile1| Activates the swap space. <B><br>System type: standard system|
94e41f4b71Sopenharmony_ci   | mksandbox | mksandbox fileName<br>Example:<br>mksandbox system| Creates a sandbox.<B><br>System type: standard system|
95e41f4b71Sopenharmony_ci   | loadcfg | loadcfg&nbsp;filePath<br>Example:<br>loadcfg /patch/fstab.cfg| Loads other <strong>.cfg</strong> files. The maximum size of the target file (only <strong>/patch/fstab.cfg</strong> supported currently) is 50 KB. Each line in the <strong>/patch/fstab.cfg</strong> file is a command. The command types and formats must comply with their respective requirements mentioned in this table. A maximum of 20 commands are allowed.<B><br>System type: small system|
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci### Available APIs
98e41f4b71Sopenharmony_ciJob management is a part of the init startup process. It is a process-based function that completely serves the init startup process. It does not provide any functional APIs for other modules. It works in a way similar to command group management and does not provide help for other types of management. The following describes the job management APIs.
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci**Table 2** Description of job parsing APIs
101e41f4b71Sopenharmony_ci| API| Function| Supported System Type|
102e41f4b71Sopenharmony_ci|:--------|:-----|:------|
103e41f4b71Sopenharmony_ci|void ParseAllJobs(const cJSON *fileRoot)|Provides the general entry for parsing jobs.| Small and standard systems|
104e41f4b71Sopenharmony_ci|static void ParseJob(const cJSON *jobItem, Job *resJob)|Checks whether a job exists and parses <strong>cmds</strong> in it.| Small system|
105e41f4b71Sopenharmony_ci|int GetCmdLinesFromJson(const cJSON *root, CmdLines **cmdLines)| Parses <strong>cmds</strong> in the job. This API is used for the small system.<br>It does not apply to the standard system because the <strong>trigger</strong> command and <strong>condition</strong> attribute are involved.| Small and standard systems|
106e41f4b71Sopenharmony_ci|int ParseTriggerConfig(const cJSON *fileRoot, <br>int (*checkJobValid)(const char *jobName))|Parses the <strong>trigger</strong> command in the job.| Standard system|
107e41f4b71Sopenharmony_ci|static int ParseTrigger_(const TriggerWorkSpace *workSpace,<br>const cJSON *triggerItem, <br>int (*checkJobValid)(const char *jobName))|Obtains the job name, condition attribute, and <strong>cmds</strong> command group.<br>Jobs are stored in a hash table, and commands are stored in a queue structure.| Standard system|
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci**Table 3** Description of the job triggering APIs
110e41f4b71Sopenharmony_ci| API| Function| Supported System Type|
111e41f4b71Sopenharmony_ci|:--------|:-----|:------|
112e41f4b71Sopenharmony_ci|void PostTrigger(EventType type, const char *content, uint32_t contentLen)|Verifies the validity of the job name and sends a job triggering event.| Standard system|
113e41f4b71Sopenharmony_ci|static void SendTriggerEvent(int type, const char *content, uint32_t contentLen)|Performs functions such as system control and starting or stopping of services based on system parameters.| Standard system|
114e41f4b71Sopenharmony_ci|static void DoTriggerCmd(const struct CmdArgs *ctx)|Executes the <strong>trigger</strong> command.| Standard system|
115e41f4b71Sopenharmony_ci|void DoTriggerExec(const char *triggerName)|Finds a command group based on the job name and pushes the commands in the command group to the execution queue.<br>This API is available only for the standard system.| Standard system|
116e41f4b71Sopenharmony_ci|void DoJob(const char *jobName)|Matches a job based on the job name and invokes <strong>DoCmdByIndex</strong><br>to execute the commands in the job.| Small system|
117e41f4b71Sopenharmony_ci|void DoCmdByIndex(int index, const char *cmdContent)|Combines parameters and commands.| Small and standard systems|
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci### Example
120e41f4b71Sopenharmony_ciThe following is the template for configuring <strong>jobs</strong> in the <strong>.cfg</strong> file. You can use it to verify the job management function.
121e41f4b71Sopenharmony_ci```
122e41f4b71Sopenharmony_ci{
123e41f4b71Sopenharmony_ci    "jobs" : [{     // Basic job
124e41f4b71Sopenharmony_ci            "name" : "stage1",
125e41f4b71Sopenharmony_ci            "cmds" : [
126e41f4b71Sopenharmony_ci                "start service1",
127e41f4b71Sopenharmony_ci                "mkdir dir1"
128e41f4b71Sopenharmony_ci            ]
129e41f4b71Sopenharmony_ci        }, {        // Conditional job
130e41f4b71Sopenharmony_ci            "name" : "param:test.name1=0",
131e41f4b71Sopenharmony_ci            "condition" : "test.name1=0",
132e41f4b71Sopenharmony_ci            "cmds" : [
133e41f4b71Sopenharmony_ci                "chmod 0755 dir1",
134e41f4b71Sopenharmony_ci                "chown root root dir1"
135e41f4b71Sopenharmony_ci            ]
136e41f4b71Sopenharmony_ci        }, {        // Custom job
137e41f4b71Sopenharmony_ci            "name" : "param:test.name2=*",
138e41f4b71Sopenharmony_ci            "condition" : "test.name2=*",
139e41f4b71Sopenharmony_ci            "cmds" : [
140e41f4b71Sopenharmony_ci                "chmod 0644 dir1",
141e41f4b71Sopenharmony_ci                "chown system system dir1"
142e41f4b71Sopenharmony_ci            ]
143e41f4b71Sopenharmony_ci        }
144e41f4b71Sopenharmony_ci    ]
145e41f4b71Sopenharmony_ci}
146e41f4b71Sopenharmony_ci```
147e41f4b71Sopenharmony_ciThe differences in job configuration are described as follows:
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci1. <strong>name</strong> and <strong>cmds</strong> are mandatory for a job, and <strong>cmds</strong> must contain commands supported by the system.
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci2. <strong>condition</strong> is an optional attribute of a job. It indicates that the job is triggered only when the specified condition is met; that is, the job will not be invoked in a specific phase by the code or the <strong>trigger</strong> command.
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci3. The job name must comply with the specified rules. For a job whose condition is a system parameter, its name is prefixed with <strong>param:</strong>.
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci4. Commands in a renamed job can be executed only after being triggered by the <strong>trigger</strong> command in other executable job command groups. By default, the <strong>trigger</strong> command is executed in the post-init phase.
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci5. An existing job name can be used in different files. Jobs with the same name are regarded as the same job. When jobs with the same name are executed, the commands in these jobs are executed together.
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci6. For a conditional job, a condition is usually a system parameter. You can set a specific value so that the job is triggered when the condition is met. You can also set the value to an asterisk (*) so that the job is triggered whenever the condition is met, regardless of the parameter value.
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci7. For the small system, the commands in a job cannot be triggered by the <strong>trigger</strong> command in the post-init phase.
162