# Execute task

An Execute task starts a program on the server: a script, a converter, a virus scanner, anything that can be run from a command line. It is the way out for whatever no other task does.

> [!WARNING]
> The program runs as the account the server itself runs under, with all that account may do. Only full administrators can put this task into a job; no role can grant it. A server in restricted mode refuses it altogether: "CRUSHFTP RESTRICTED MODE BLOCKS EXECUTE."

## Fields

| Field | What it does | On a new task |
|---|---|---|
| **Command** | the program: its name, or its full path. For a script, the program that runs it: `/bin/bash`, `cmd.exe`, `python3`. | |
| **Argument** | what is handed to the program, the single arguments separated by the **Separator** | |
| **Separator** | the character that parts the arguments | `;` |
| **Working Directory** | the folder the program is started in | |
| **Environment Variables** | `NAME=value` pairs for the program, separated by `;`. Rarely needed. | |
| **Ignore Error Result** | the task does not fail when the program reports an error | off |
| **Execute on DMZ** | the program is started on a DMZ server, not here | |
| **Examples** | fills in a sample: copy to an archive folder, move to a hidden folder, run a program of your own | |

The task runs the program **once per item** its filter matches, and once when the list is empty.

## Why the separator

> Example: If the command was "cp" and the argument was "{real_path};/archive/{name}"
> Then, if a file was uploaded to "/uploads/stuff.zip", it would be copied to "/archive/stuff.zip"
> Note the purpose of the ";" (semicolon) in that, it separates arguments to the command.

Arguments are not parted at spaces, as a shell would do it, but at the separator. That is on purpose: a file named `my report.pdf` stays one argument, and nothing in a file's name can turn into a second command. The program is started directly; there is no shell in between, so `|`, `>` and `&&` mean nothing.

To use a shell's features, start the shell: **Command** `/bin/bash`, **Argument** `-c;gzip -c "$0" > "$0.gz";{real_path}`. Hand file names over as arguments of their own, as here, never pasted into the shell's command text.

## Variables

All [variables](Jobs-Variables.md) work. The ones this task needs most:

| Variable | Is |
|---|---|
| `{real_path}` | the file's path as the operating system writes it. Only for files on the server's own disk. |
| `{real_parent_path}` | its folder |
| `{name}`, `{stem}`, `{ext}` | parts of its name |

A program can only work on what is on the server's disk. A file on another server has to be copied here first.

## What the program gives back

| | |
|---|---|
| what it printed | is written to the job's log, and is `{execute_log}` on the item, and `{last_execute_log}` for the whole run |
| its exit code | is `{error_code}`. Anything but 0 fails the task, "Process exit error: 1", unless **Ignore Error Result** is ticked. |

So a script can report to the job: print a value, and a [UserVariable](Task-UserVariable.md) or a [Jump](Task-Jump.md) behind the task reads it from `{execute_log}`.

## What it hands on

The items it was given, each with its `{execute_log}` and `{error_code}`. Files the program made are not on the list: put a [Find](Task-Find.md) behind it for those.

## Examples

| To | Command | Argument |
|---|---|---|
| scan a file for viruses | `/usr/bin/clamdscan` | `--no-summary;{real_path}` |
| convert a picture | `/usr/bin/convert` | `{real_path};-resize;800x800;{real_parent_path}{stem}_small.jpg` |
| run a script of your own | `/bin/bash` | `/opt/scripts/after-upload.sh;{real_path};{user_name}` |
| a Windows batch file | `cmd.exe` | `/c;C:\scripts\after.bat;{real_path}` |

A virus scanner reports a find by its exit code: lead the task's red line to a Move into a quarantine folder and a mail.

## Things to know

- **Stopping.** When the run is stopped, the program is ended.
- **Time.** A program that never ends holds the run. Give the job a **Max runtime**.
- **Names from outside.** File names come from users. They reach your program as arguments, exactly as they are, with spaces, quotes and dashes. A script must not paste them into another command line, and should put `--` before them where a program takes it, so that a name beginning with `-` is not read as an option.
- **On another machine.** With [engines on other machines](Jobs-Engines-Other-Machines.md) the program is started where the run executes. Use a label to keep such a job on the machine that has the program.
- **Before reaching for Execute**, look whether a task does it: [Zip](Task-Zip.md), [Compress](Task-Compress.md), [PGP](Task-PGP.md), [HTTP](Task-HTTP.md), [SQL](Task-SQL.md) cover most of what scripts used to be written for.
