In OpenSSH's sftp, how does batch mode with sftp -b change error handling, and how should a CI job's batch file use that?
answer
- what a script needs from a failure
- the abort list in the sftp man page
- a leading dash on one line
- the exit status the CI step sees
- no one there to type
basics
~20 sWith sftp -b, OpenSSH's sftp stops at the first failing file command (put, rename, rm, mkdir and others) and exits non-zero, so the CI step fails; a leading '-' tolerates one line's failure, and authentication must need no typing.
solid answer
~40 s`sftp -b batchfile` reads commands from a file (or from stdin with `-b -`) instead of a terminal, and it changes the failure rule: sftp **aborts** as soon as one of the file commands its man page lists — `get`, `put`, `rename`, `rm`, `mkdir`, `ls` and the rest — fails, and the process ends with a non-zero status, which is what turns the CI step red. A leading `-` on a line (`-rm incoming/settle.csv.part`) suppresses that termination for that line only, so I use it for cleanup whose failure is harmless and never on `put` or `rename`; a leading `@` only hides the echo. Because nobody is there to type, the job uses a key and `-o BatchMode=yes`, so any password, passphrase or host-key prompt becomes an immediate failure.
code
bash · 8 linessftp -b - -o BatchMode=yes -i "$DEPLOY_KEY" [email protected] <<'EOF'
-rm incoming/settle-20261006.csv.part
put out/settle-20261006.csv incoming/settle-20261006.csv.part
rename incoming/settle-20261006.csv.part incoming/settle-20261006.csv
EOF
status=$?
echo "sftp exit status: $status"
exit "$status"go deeper
Remember that sftp -b stops at the first failing file command and exits non-zero, and that batch mode needs key-based login because nobody can type a password.
Explain the abort list, the difference between the '-' and '@' prefixes, and why the non-zero exit status is what makes the CI step fail.
Show a batch that is safe to rerun: tolerated cleanup, upload under a temporary name, rename last, and a clear story for a failure after the rename.
Weigh shelling out to sftp -b against a library for many partner integrations: exit codes and echoed logs versus structured exceptions, retries and per-step checks.
## What batch mode is OpenSSH's `sftp` is an interactive client for the **SSH File Transfer Protocol**: it opens an SSH connection, starts the SFTP subsystem on the server, and then reads commands such as `put`, `get`, `rename` and `ls` from the keyboard. With `-b batchfile` it reads those commands from a file instead; `-b -` reads them from standard input, which lets a shell script feed them through a here-document. Batch mode exists for unattended jobs — a nightly push of settlement files to a bank's SFTP server from CI, for example. What matters in such a job is not that the commands run, but that **a failure is impossible to miss**. ## The abort rule The sftp man page states the rule precisely: sftp aborts if any of a listed set of commands fails. The list covers the commands that touch files and directories: - transfers: `get`, `put`, `reget`, `reput` - changes on the server: `rename`, `ln`, `rm`, `mkdir`, `chmod`, `chown`, `chgrp`, `symlink`, `copy` / `cp` - navigation and inspection: `chdir`, `ls`, `df` - local helpers: `lchdir`, `lpwd`, `lmkdir` So a batch whose `put` fails — the disk on the server is full, the target folder does not exist, the account may not write there — stops at that line. The `rename` after it never runs, which is exactly what you want: a half-sent file is never promoted to its final name. ## The two line prefixes | Prefix | Example | Effect | |---|---|---| | `-` | `-rm incoming/settle.csv.part` | a failure on this line does **not** end the batch | | `@` | `@ls incoming` | the command is not echoed to the output | | both | `-@ls incoming` | either order works | The `-` prefix is a deliberate exception, and it should stay rare. Good uses are cleanup steps whose failure is harmless — removing a leftover temporary file that may not exist. Putting `-` on `put` or `rename` silently converts a failed delivery into a run that looks finished. ## Exit status and what CI sees When sftp aborts, the process exits with a **non-zero status**. A CI runner treats any non-zero exit of a step as a failure, so the abort rule is what connects "the server refused the upload" to "the pipeline is red". A connection or authentication failure also ends sftp with a non-zero status, before any batch line has run — the `-` prefix has no say over that. Because each command is echoed as it runs (unless prefixed with `@`), the job log shows the last command attempted followed by the server's error, which tells the on-call engineer which step broke. Two limits are worth stating plainly: 1. A zero exit means the listed commands succeeded on the server. It does not mean the partner's consumer has processed the file. 2. sftp does not retry anything. Retrying is the wrapper's job, and re-running the whole batch is safe only if the batch is written to be idempotent — upload under a temporary name, then rename. ## Authentication and prompts The man page says batch mode "should be used in conjunction with non-interactive authentication": there is no user to type a password or a key passphrase. In practice that means a dedicated key for the transfer account plus the SSH client option `BatchMode=yes`, passed with `-o`. `BatchMode=yes` disables password prompts and host-key confirmation requests, so anything that would have asked a question fails at once instead. That pairs naturally with a pinned `known_hosts` file, so the server's identity is checked rather than confirmed by a human. ## A batch file shaped for retries ```bash sftp -b - -o BatchMode=yes -i "$DEPLOY_KEY" [email protected] <<'EOF' -rm incoming/settle-20261006.csv.part put out/settle-20261006.csv incoming/settle-20261006.csv.part rename incoming/settle-20261006.csv.part incoming/settle-20261006.csv EOF ``` Line by line: the first line clears a temporary file a previous failed run may have left, and its `-` makes "nothing to remove" harmless; the `put` writes under a name the consumer ignores; the `rename` publishes the finished file. If the `put` fails, the batch aborts before the `rename`, and the next run starts clean. The date-stamped final name is unique per business day, so the rename normally never meets an existing file — what a retry should do when it does is a design question of its own. ## Common mistakes - Prefixing every line with `-` "so the job finishes", which hides every delivery failure. - Reading `@` as error suppression; it only suppresses the echo. - Relying on a password in the environment; batch mode has nobody to answer the prompt. - Treating exit status 0 as proof the partner received and processed the file.
- Which failures can the leading '-' prefix not tolerate?Anything that happens before the batch starts. A refused host key, a failed authentication or an unreachable server ends sftp with a non-zero status before line one runs. The prefix also covers only its own line: `-rm` tolerates a missing temporary file, but the `put` after it still aborts the batch if it fails.
- How do you find out from the CI log which batch line failed?sftp echoes each command before running it unless the line starts with `@`, so the last echoed command followed by the server's error message identifies the step. Keeping `@` off the transfer lines is therefore worth it; reserve it for noisy inspection commands such as a listing you only need when debugging.
- Can you simply re-run the whole batch when it fails?Only if the batch is idempotent. Uploading to a temporary name, removing a leftover one with a tolerated `-rm`, and renaming last means a rerun after a failed `put` starts clean. A failure after the rename is different: the file was delivered, and the retry must recognise that rather than deliver it twice.
- sftp -b keeps going after a failed put and reports success at the end
- A leading dash on put is a sensible way to keep the batch running
- The @ prefix suppresses errors on that line
- A batch file can answer a password prompt on the next line
- Exit status 0 from sftp proves the partner processed the file