github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/doc/content/en/docs/javascript/execute.md (about)

     1  
     2  ---
     3  title: "Execute"
     4  linkTitle: "execute"
     5  date: 2021-11-19
     6  description: >
     7  
     8  ---
     9  
    10  The **Smart Home** project's system provides the ability to execute arbitrary files and scripts synchronously and asynchronously.
    11  
    12  {{< alert color="success" >}}This function is available in any system script.{{< /alert >}}
    13  
    14  To achieve this, the following methods are available:
    15  
    16  1. `ExecuteSync(file, args)`: This method allows you to execute files and scripts synchronously. You pass the file name or path to the script as the `file` argument and any required arguments as an object `args`. Here's an example:
    17  
    18     ```javascript
    19     const file = 'script.js';
    20     const args = { param1: 'value1', param2: 'value2' };
    21  
    22     ExecuteSync(file, args);
    23     ```
    24  
    25  2. `ExecuteAsync(file, args)`: This method allows you to execute files and scripts asynchronously. The `file` and `args` arguments have the same structure as in the `ExecuteSync` method. Here's an example:
    26  
    27     ```javascript
    28     const file = 'script.js';
    29     const args = { param1: 'value1', param2: 'value2' };
    30  
    31     ExecuteAsync(file, args);
    32     ```
    33  
    34  Both the `ExecuteSync` and `ExecuteAsync` methods provide the ability to execute arbitrary files and scripts in your **Smart Home** project. The synchronous mode means that the code execution will block subsequent operations until the script is completed, while the asynchronous mode allows you to continue executing other operations without waiting for the script to finish. You can use these methods for integration with other systems, executing custom scripts, or launching external applications in your **Smart Home** project.
    35  
    36  ----------------
    37  
    38  ### Code Example
    39  
    40  ```coffeescript
    41  # ExecuteSync
    42  # ##################################
    43  "use strict";
    44  
    45  r = ExecuteSync "data/scripts/ping.sh", "google.com"
    46  if r.out == 'ok'
    47    print "site is available ^^"
    48  ```
    49  
    50  ping.sh
    51  ```bash
    52  #!/usr/bin/env bash
    53  
    54  ping -c1 $1 > /dev/null 2> /dev/null; [[ $? -eq 0 ]] && echo ok || echo "err"
    55  ```