wowprompt

Bash script with safe error handling for a server task

Writes a Bash script for a routine server job such as a backup, log cleanup or file sync, with strict mode, input checks, a lock against double runs, logging and a dry-run option. Use it when a one-line command is about to become a cron job and needs to fail safely.

A commented Bash script in a code block with a usage line, a dry-run flag and logging, followed by a cron line and a test checklist
The output this prompt produced, unedited.

The prompt

Goal

A script that does one server task reliably, stops cleanly on any error, and never deletes or overwrites more than it should.

Inputs

  • The task in plain words: [e.g. compress logs older than 7 days in /var/log/myapp and delete archives older than 30 days]
  • Operating system and shell: [e.g. Ubuntu 24.04, Bash 5]
  • Paths, users and tools available: [e.g. rsync, tar, gzip; runs as a service user, not root]
  • How often it runs: [e.g. daily at 02:30 by cron]
  • Where output should go: [log file path, email, or standard output only]

Steps

  1. Start with a shebang and strict mode: set -euo pipefail, and a safe IFS.
  2. Parse arguments, including --dry-run and --help, and validate every path before use.
  3. Take a lock with flock so two runs never overlap.
  4. Do the work, logging each step with a timestamp; in dry-run, print actions instead of running them.
  5. Trap errors and exit with a non-zero code and a clear message.

Output format

  • The full script in one fenced code block, with short comments above each section.
  • A usage line and two example commands, one with --dry-run.
  • The crontab line, with output redirected to the log.
  • A test checklist: empty folder, missing folder, no permission, disk full, and a second run while the first is still going.

Check before answering

  • Every variable is quoted, and rm or find -delete only ever runs on a checked, non-empty path.
  • No passwords or tokens in the script; read them from an environment variable or a protected file.
  • Commands used exist on the stated system, with no GNU-only flags if the target is not GNU.
bash scriptshell scriptcron job scriptserver automationbash error handlingbackup script linux

How to run this

Copy the prompt and paste it into Gemini, then set the aspect ratio to 3:4. No reference photo is needed for this one.

The prompt is written in Markdown with each part under its own heading, which is what the copy action hands over. Change one section at a time, keeping the rest intact, so you can tell which change did the work.

Related prompts

All productivity