Automation & scripting
Script recipes
Short patterns for common jobs, such as newest file, safe uploads, retries, streaming and several servers.
Each recipe is a script fragment or a shell wrapper you can adapt. They
assume a stored site called files or a URL with -hostkey, as explained in
sessions and credentials, and the error
handling from exit codes.
Fetch only the newest file
Section titled “Fetch only the newest file”-latest picks the most recently modified file among those that match:
open filesget -latest /exports/sales-*.csv /home/alice/in/exitName the download after the date
Section titled “Name the download after the date”get /var/log/app.log /home/alice/logs/app-%TIMESTAMP#yyyy-mm-dd%.logUpload without exposing half-written files
Section titled “Upload without exposing half-written files”Readers on the server should never pick up a file that is still arriving. Upload under a temporary name, then rename it in one step:
put /home/alice/out/report.csv /incoming/report.csv.partmv /incoming/report.csv.part /incoming/report.csvDownload only when the sender is done
Section titled “Download only when the sender is done”Many senders drop a marker file after the data. stat fails when the marker
is missing, which stops the script with exit code 1 before anything is
fetched:
open filesstat /exports/ready.flagget /exports/*.csv /home/alice/in/rm /exports/ready.flagexitIn the wrapper, treat code 1 as “try again later”, not as an alarm.
Archive local files after a successful upload
Section titled “Archive local files after a successful upload”Let linscp-cli delete the sources as it uploads them with put -delete, or
keep them and move them from the shell once the whole run has succeeded:
if linscp-cli /nointeractiveinput /script=upload.txt /log=upload.log; then mv /home/alice/out/*.csv /home/alice/sent/fiRetry a flaky connection
Section titled “Retry a flaky connection”#!/bin/shfor attempt in 1 2 3 4 5; do linscp-cli /nointeractiveinput /script=job.txt /log=job.log && exit 0 echo "attempt $attempt failed" >&2 sleep $((attempt * 60))doneexit 1Inside one run, option reconnecttime already covers short drops.
Upload a list of files
Section titled “Upload a list of files”Build the script from a list, one path per line, then run it:
{ echo 'open files' echo 'cd /incoming' while IFS= read -r f; do printf 'put "%s"\n' "$(printf '%s' "$f" | sed 's/"/""/g')" done < /home/alice/to-send.txt echo 'exit'} > /tmp/send.txtlinscp-cli /nointeractiveinput /script=/tmp/send.txtThe sed doubles any double quote in a name, as script quoting requires.
Same job, several servers
Section titled “Same job, several servers”Pass the site as a parameter and loop in the shell:
for site in web1 web2 web3; do linscp-cli /nointeractiveinput /script=deploy.txt /parameter // "$site" \ || echo "deploy to $site failed" >&2doneopen %1%synchronize remote -delete /home/alice/site/public /var/www/htmlexitStream to and from other programs
Section titled “Stream to and from other programs”With /stdout, get writes the file to standard output when the target is
-. Status messages move to standard error. This backs up a remote database
dump without a temporary file:
linscp-cli /stdout /command 'open files' 'get /backups/db.sql -' 'exit' \ | gzip > /home/alice/backups/db-$(date +%F).sql.gzWith /stdin, put - reads the data from standard input:
tar -czf - -C /home/alice site | linscp-cli /stdin /command 'open files' 'put - /backups/site.tar.gz' 'exit'Mail the result
Section titled “Mail the result”if ! linscp-cli /nointeractiveinput /script=job.txt /log=job.log; then tail -n 50 job.log | mail -s "linscp job failed on $(hostname)" alice@example.comfiSee the command reference for every switch used here.