如何在Bash脚本中带参数运行godaddy-ddns脚本并保存输出至日志?
Hey there! Let's walk through how to run that GoDaddy DDNS Python script with parameters from a Bash shell, plus how to save its output to a log file for later checking.
First, make sure you've got the script saved locally. To pass parameters to it from Bash, you just append them directly to the python command like you would with any CLI tool.
For example, if the script requires your GoDaddy API key, secret, and target domain, your command would look something like this:
python3 godaddy_ddns.py --key YOUR_GODADDY_API_KEY --secret YOUR_GODADDY_API_SECRET --domain your-awesome-domain.com
- Replace
YOUR_GODADDY_API_KEYandYOUR_GODADDY_API_SECRETwith your actual credentials from GoDaddy's developer portal. - If the script isn't in your current working directory, use the full path to it (e.g.,
/home/your-user/scripts/godaddy_ddns.py).
There are a few ways to capture the script's output—pick the one that fits your needs:
Basic File Redirection
This is the simplest method: redirect both standard output and error messages to a log file. Use >> to append new output to the file (instead of overwriting it each time), and 2>&1 to make sure error messages go to the same log as regular output.
Example command:
python3 godaddy_ddns.py --key YOUR_API_KEY --secret YOUR_API_SECRET --domain your-domain.com >> /var/log/godaddy_ddns.log 2>&1
If you want to overwrite the log file each time the script runs (instead of appending), replace >> with a single >.
Real-Time Log Monitoring
If you want to watch the script's output as it runs (great for debugging), you can run the script normally and pipe the output to tee—this sends output to both your terminal and the log file:
python3 godaddy_ddns.py [your-parameters] 2>&1 | tee -a /var/log/godaddy_ddns.log
The -a flag tells tee to append instead of overwrite.
Send Output to System Logs
For more integrated logging (so you can use system tools like journalctl or grep to filter logs), use the logger command. This tags your script's output so you can easily find it later:
python3 godaddy_ddns.py [your-parameters] 2>&1 | logger -t godaddy-ddns
To view these logs, you can run:
grep godaddy-ddns /var/log/syslog # Or if you use systemd: journalctl -t godaddy-ddns
Rotate Logs to Avoid Bloat
If you run the script frequently (like via a cron job), your log file can get huge over time. Use logrotate to automatically archive old logs and keep things tidy.
- Create a logrotate config file, e.g.,
/etc/logrotate.d/godaddy_ddns:
/var/log/godaddy_ddns.log { daily # Rotate logs every day rotate 7 # Keep the last 7 days of logs compress # Compress old logs with gzip missingok # Don't throw an error if the log file is missing notifempty # Don't rotate if the log file is empty }
logrotateruns automatically on most systems, but you can test it manually with:
logrotate -v /etc/logrotate.d/godaddy_ddns
To make running the script easier (especially for scheduled updates), wrap it in a simple Bash script:
- Create a file named
ddns_update.sh:
#!/bin/bash # GoDaddy DDNS Update Script python3 /full/path/to/godaddy_ddns.py --key YOUR_API_KEY --secret YOUR_API_SECRET --domain your-domain.com >> /var/log/godaddy_ddns.log 2>&1
- Make it executable:
chmod +x ddns_update.sh
- Add it to your crontab to run every 15 minutes (adjust the interval as needed):
crontab -e
Add this line at the bottom:
*/15 * * * * /full/path/to/ddns_update.sh
内容的提问来源于stack exchange,提问作者Harun Reşit Ç

