My FNZ Studio Version

Scripting

Latest Update: 11.04.2025 |
Recipe
2024
2023
2022.2
2022.1
11
10
9
8
7

Problem

How do I use the EXEC Function, used to execute arbitrary system commands?

Solution

IMPORTANT: The EXEC function has moved from Appway Core to the UnprotectedExecutionFunctions extension.

If you want to continue using this function, install the UnprotectedExecutionFunctions extension when upgrading to Appway 7.

Introduction

The expression function EXEC may be used to execute arbitrary system commands. Input may be read from a file, output and error messages may be redirected to STDOUT or STDERR, or redirected to files.

Parameters

There are one mandatory parameter, four optional parameters and an unlimited number of argument parameters for the system command.

  • command
    The first parameter is the only mandatory parameter. It is used to pass the name of the system command to be executed. This parameter must not include command arguments or 'shell features' like redirections.

    Note: You may have to specify the absolute path to the command's executable file.

    Examples:
    • ping.exe
    • C:/ERP/bin/ImportOrder.exe
    • passwd
    • /usr/sbin/adduser
  • wait
    This parameter is used to instruct the function whether it has to wait until the system command has been completed, or whether the command has to be executed in the background.

    A value of true means that the function suspends expression execution until the system command has been executed. Using this for long-running commands may result in situations where users think that the server is not responding anymore.
    If you pass false, the function starts a background thread in order to execute the system command. The function immediately returns and expression execution continues in parallel to the system command execution.

    One important difference is that if you wait for the system command to complete, the function returns the system command's result code. If you execute the command in the background, the function always returns 0.
    The default value for this parameter is true (wait for command completion).

  • input
    Some system commands (mainly on Unix or Linux systems) may read input data from an input stream. If you would like to pass input data to the command, you have to write this data into a file and use this parameter to pass the file path to the function. If your command doesn't need any input data, pass null for this parameter.

    The default value for this parameter is null.

  • output
    Output produced by the system command may be discarded, redirected to STDOUT or STDERR, or written to a file. If you pass null for this parameter, output is discarded. If you pass STDOUT or STDERR, output is redirected to the corresponding stream. All other values are interpreted as file paths and output is written to this file.

    If you redirect output to a file and this file doesn't exist, it is created. If the parent directory of the file doesn't exist, an error is thrown. If the file already exists, it is overwritten.

    If you redirect output to STDOUT or STDERR, it is most likely that it will be written to a log file of the Java Application Server, because servers tend to redirect these streams to files.

    If you specify a relative file path, this path is interpreted relative to the Appway Data Home directory.

    The default value for this parameter is null (discard output).

  • error
    This parameter may be used to redirect error messages. Like for parameter output, null may be used to discard error messages, STDOUT and STDERR may be used to redirect to the corresponding system streams, and you may specify a file path to redirect error messages to a file. In addition to these values, OUT may be specified to redirect error messages to the same location like default output.

    This parameter supports the same values like the output parameter described above: null, STDOUT, STDERR or a file path. In addition to this, you may pass the value OUT to redirect error messages to the same location the default output is written.

    Note: If you would like to redirect output and error messages to the same file, don't specify the same file path for both parameters. This may lead to unexpected results. Instead, pass the file path as output parameter and specify the special value OUT for the error parameter.
    The default value for this parameter is null (discard error messages).
  • arguments
    Many system commands require arguments. You can pass them as individual parameters.

Return Value

If the function is instructed to wait until the system command has been executed, the return code of the command is returned. This value is platform and/or command dependent. A value of 0 usually means 'command completed successfully'. All other values indicate some kind of error.

If the function is instructed to execute the system command in the background, it always returns 0.

Examples

The following expression will execute the ping.exe command on Windows systems, sending ICMP packets to the host www.numcom.com and writing the output to the file ping.txt (inside the Appway Data Home directory).

EXEC('ping.exe', true, null, 'ping.txt', null, 'www.numcom.com')

If the host can be found, 0 is returned. This is the output written to the file:

Pinging www.numcom.com [80.86.200.36] with 32 bytes of data:
Reply from 80.86.200.36: bytes=32 time=21ms TTL=121
Reply from 80.86.200.36: bytes=32 time=19ms TTL=121
Reply from 80.86.200.36: bytes=32 time=17ms TTL=121
Reply from 80.86.200.36: bytes=32 time=17ms TTL=121

Ping statistics for 80.86.200.36:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 17ms, Maximum = 21ms, Average = 18ms

If you change the host name to point to an non-existing host (e.g. foobar.numcom.com), a value different from 0 is returned (1 on my system).

The output file now contains an error message:

Ping request could not find host foobar.numcom.com. Please check the name and try again.

The following expression may be used to create a user account on a Unix or Linux system. In this example, the user gandalf, with a home directory /home/gandalf, is created. Output and error messages are discarded.

EXEC('/usr/sbin/adduser', true, null, null, null, '--home', '/home/gandalf', 'gandalf')

In order to call scripts (Visual Basic scripts, shell scripts etc.) you may have to pass the script interpreter as system command and the path to the script file as argument.

EXEC('C:\\WINDOWS\\system32\\cscript.exe', true, null, null, null, 'D:\\scripts\\DailyBackup.vbs')

The following example starts a Java application deployed as JAR file SMSGateway.jar in the background.

EXEC('java', false, null, 'output.txt', 'error.txt', '-jar', 'SMSGateway.jar')

Privileges

All system commands are executed using the user account the Java Application Server is running with. You may need to use workarounds like sudo if you don't have the required privileges to execute a command.

Error Handling

There are two types of errors:

  • Errors generated by the EXEC function in case of illegal or unexpected parameters.
  • Errors occurring during command execution.

Errors of the first type usually result in exceptions being thrown. If the system command is unknown, an IOException with a text like the following is thrown:

java.io.IOException: CreateProcess: RebootServer error=2

Other exceptions may be thrown if you specify a non-existing input file or if output or error messages should be redirected to files where the parent directory doesn't exist.

Errors of the second type should result in a return value different from 0. Check the error messages of the system command in order to find the reason for the error. If the system command is executed in the background, this might be the only way to check whether the command has completed successfully.

Known Issues

Permission denied

When running on Linux or MacOS, the JDK may rely on a helper library $JAVA_HOME/lib/jspawnhelper to start a system process. If the user used to run the Java application server (e.g., Tomcat) does not have execute permission on this library file, the EXEC function may throw the following exception:

java.io.IOException: error=13, Permission denied

The solution is to make sure that the user has execute permission, for example with this command:

chmod a+x $JAVA_HOME/lib/jspawnhelper
© 2026 Appway AG. All Rights Reserved