Skip to content

ChatGPT Prompts for Writing Bash Scripts and Cron Jobs (UK Edition)

This page may contain affiliate links.

Let's be honest: most of us didn't learn bash because we wanted to. We learned it because we got sick of manually renaming files, or because our Raspberry Pi kept filling up its SD card at 2am. And cron? Cron is that mysterious thing your sysadmin friend mutters about while sipping a flat white. But here's the good news: you don't need a computer science degree to write decent scripts anymore. You just need to know how to ask ChatGPT the right questions. In this guide, I'll share the exact prompts I use to generate, debug and schedule bash scripts — with UK-specific examples that actually matter (backups, energy tariffs, that sort of thing). No fluff, just copy-paste-and-adapt.

Why ChatGPT Beats Stack Overflow (Mostly)

Stack Overflow is brilliant, but it's a rabbit hole. You search for 'cron job not running', get 47 answers, and half of them assume you're running CentOS from 2012. ChatGPT gives you a tailored answer in seconds. The trick is to treat it like a junior developer who needs very clear instructions. Give it context: your OS (Ubuntu, Debian, macOS), your shell (bash, zsh), and what you're trying to achieve. The more specific you are, the better the output. For a UK audience, that means mentioning things like your backup drive is an external SSD, or you want to scrape a UK energy tariff page at 6am. Don't be shy — ChatGPT loves details.

Prompt 1: The 'Write Me a Script' Prompt That Actually Works

Most people type 'write a backup script' and get something generic that doesn't run. Instead, try this structured prompt:

"You are a senior Linux sysadmin. Write a bash script for Ubuntu 22.04 that backs up the directory /home/user/Documents to an external drive mounted at /mnt/backup. The script should: 1) check the external drive is mounted, 2) create a timestamped tar.gz archive, 3) delete backups older than 14 days, 4) log all output to /var/log/backup.log, 5) send a desktop notification on failure using notify-send. Use set -euo pipefail. Explain each section briefly."

That prompt gives you a production-ready script, not a toy. The set -euo pipefail bit is crucial — it makes the script fail loudly instead of silently doing the wrong thing. I've used this exact pattern to generate scripts for everything from syncing a Plex library to rotating Let's Encrypt certificates. If the first output has an error, just paste the error message back and say 'fix this'. It's like having a patient colleague who never sighs.

Prompt 2: Debugging Your Broken Script (Without Losing Your Mind)

We've all been there: the script worked yesterday, and now it throws command not found or a cryptic syntax error near unexpected token. Instead of staring at line 47 for an hour, use this prompt:

"Here is a bash script that was working but now fails. The error is: [paste error]. The script is: [paste script]. What is the most likely cause given that I'm running Ubuntu 22.04 and the script is called from cron? Give me the fix and explain why it happened."

Nine times out of ten, the culprit is PATH. When you run a script manually, your shell has your full PATH. But cron runs with a minimal environment — /usr/bin:/bin — so if your script calls ffmpeg or python3 installed in /usr/local/bin, it fails. ChatGPT will spot that instantly. Another classic: line endings. If you've edited the script on Windows or copied it from a web page, stray carriage returns (\r) cause havoc. Ask ChatGPT to check for that too. It'll save you a genuine headache.

Prompt 3: Cron Jobs That Actually Run (And Don't Email You at 3am)

Cron syntax is easy to get wrong, especially the day-of-week bit (0 is Sunday, not Monday — I still forget). Here's my go-to prompt for scheduling:

"I need a cron job on Ubuntu 22.04 that runs my script /home/user/scripts/energy_check.sh every weekday at 7:30am UK time. It should only run if the system is on AC power (not battery). Write the crontab entry and also show me how to redirect all output to a log file, and how to suppress the default email that cron sends."

That last bit is important — if you don't redirect output, cron emails you every single run. For a daily job, that's fine. For a job that runs every 5 minutes, your inbox will hate you. The AC power check is a nice touch for laptop users; you can use on_ac_power or check /sys/class/power_supply/AC/online. Also, always use absolute paths in cron — /home/user/scripts/energy_check.sh, not ~/scripts/.... ChatGPT will remind you of this, but it's worth knowing yourself. And if you're scheduling something that needs the network (like a download), add a sleep 30 at the start of the script to give Wi-Fi time to connect after boot.

Prompt 4: Making Scripts UK-Friendly (Dates, Currency, and Energy Tariffs)

Here's where a generic US-centric script will trip you up. UK dates are DD-MM-YYYY, not MM-DD-YYYY. And if you're scraping a price or tariff, you need to handle the £ symbol and decimal commas properly. Try this prompt:

"Write a bash script that checks the current electricity unit rate from the Octopus Energy Agile API (or any UK supplier's public API). The script should parse the JSON, extract the current rate in pence per kWh, and if it's below 15p, log a message and send a notification. Use jq for parsing. Format the date as DD-MM-YYYY HH:MM. Handle the £ sign correctly. Assume the API returns rates as decimal numbers."

This is genuinely useful if you're on a time-of-use tariff — you can run it every 30 minutes via cron and get a notification when it's cheap to charge your EV or run the tumble dryer. ChatGPT will generate the jq filter for you, which is a godsend if JSON makes your eyes glaze over. Just be careful to verify the API endpoint yourself; ChatGPT might hallucinate a URL. Ask it to explain the parsing logic so you can adapt it to your actual API response.

Prompt 5: Turning a Messy One-Liner into a Maintainable Script

We all have that one-liner we found on a forum that does something magical but is completely unreadable. Something like find . -type f -name '*.log' -mtime +7 -exec gzip {} \;. It works, but you can't remember why. Use this prompt to clean it up:

"Take this bash one-liner: [paste]. Rewrite it as a well-commented, multi-line bash script with functions, error handling, and a usage message. Add a --dry-run flag so I can test it safely. Use UK English in comments."

The --dry-run flag is a lifesaver. It prints what the script would do without actually doing it. I've lost count of how many times that's saved me from deleting the wrong folder. ChatGPT will also add a set -u to catch unset variables, and maybe a trap to clean up temp files. The result is a script you can actually maintain in six months when you've forgotten why you wrote it.

Now, if you're thinking 'this all sounds great, but I'd rather have a pack of ready-made prompts than type all this out myself' — I've got you. I've put together a Home Lab & Automation Pack with done-for-you prompts for scripting, troubleshooting and documenting your home lab. It's from £9, and it covers everything from the prompts above to more advanced stuff like Docker and network monitoring. Think of it as the cheat sheet I wish I'd had when I started — no more staring at a blank terminal wondering what to ask.

Common Pitfalls and How to Avoid Them

Let's wrap up with a few traps I see constantly in UK home labs. First, timezone: cron uses the system timezone, so if your Pi is set to UTC but you want 8am BST, you'll be an hour off in summer. Set your system to Europe/London with sudo timedatectl set-timezone Europe/London. Second, permissions: if a script works when you run it with sudo but fails in cron, check that the user cron runs as has execute permissions on the script and write permissions on the log directory. Third, quoting: if your script has spaces in file paths (like 'My Documents'), always quote them — "$file" not $file. ChatGPT will handle this if you mention it, but it's worth checking its output. Finally, test your cron job by running the script manually first, then run crontab -l to verify the entry, and check grep CRON /var/log/syslog after it should have run. That log file is your best friend for debugging.

There you have it — five prompts that will take you from bash novice to someone who confidently schedules scripts at 2am without fear. The key is to be specific, paste your errors, and always ask for a dry-run option. ChatGPT isn't perfect — it'll occasionally suggest a flag that doesn't exist or forget that you're on a Raspberry Pi — but with a bit of back-and-forth, you'll get there. And when you do, you'll wonder how you ever lived without a script that backs up your photos every night while you sleep. Happy scripting, and remember: always test on a copy first.

Written by

Richard Tucker

View all posts →