Deploy Shell Scripts with Git and Cron
Manage scripts using Git and schedule with cron.
Modern system administration often requires automating repetitive tasks and ensuring that these automation scripts are version-controlled and easy to update. The combination of Git and cron provides a powerful workflow for deploying and managing shell scripts on servers. This article will guide you through using Git to manage deployment and cron to handle scheduled execution.
Why Use Git to Manage Shell Scripts?
Shell scripts are essential for automating system maintenance, backups, data processing, and more. Managing these scripts with Git offers several significant benefits:
- Version Control: Track changes, revert to previous versions, and collaborate with others.
- Backup: Secure your scripts against accidental deletion.
- Deployment: Clone or pull updates across multiple servers seamlessly.
- Documentation: Maintain readmes and inline explanations within the repository.
Setting Up Your Repository
First, create a Git repository for your shell scripts. You can use a platform like GitHub, GitLab, or a self-hosted Git server.
# Locally initialize and structure your repository
mkdir my-shell-scripts
cd my-shell-scripts
git init
# Add your scripts
echo '#!/bin/bash\necho "Hello, World!"' > hello.sh
chmod +x hello.sh
git add hello.sh
git commit -m "Add hello script"
Push this repository to your remote of choice:
git remote add origin git@github.com:yourusername/my-shell-scripts.git
git push -u origin master
Deploying Scripts on the Server
Clone the repository on the target server:
cd /opt
sudo git clone git@github.com:yourusername/my-shell-scripts.git
cd my-shell-scripts
Tip: It's recommended to use a deployment-specific SSH key for secure and limited access.
To update scripts after changes:
cd /opt/my-shell-scripts
git pull origin master
Automate updates with a simple cron job (optional):
# Pull latest script changes every hour
0 * * * * cd /opt/my-shell-scripts && git pull origin master >> /var/log/script_update.log 2>&1
Scheduling Execution with Cron
The cron
scheduler allows you to automate the execution of your shell scripts at defined intervals.
Basic Cron Syntax
* * * * * /path/to/command arg1 arg2
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
Scheduling Your Script
Suppose you want to run hello.sh
every day at midnight. Edit the crontab:
crontab -e
Add:
0 0 * * * /opt/my-shell-scripts/hello.sh >> /var/log/hello.log 2>&1
Tip: Always provide full paths in cron jobs for scripts and file locations.
Environmental Considerations
- Path: Cron uses a minimal
$PATH
. Explicitly reference binaries if your script uses them. - Permissions: Ensure your scripts are executable (
chmod +x script.sh
). - Shebang: Use the correct shebang (e.g.,
#!/bin/bash
) at the start of your scripts. - Outputs: Always log output and errors as shown above to debug failures.
Updating Scripts Safely
When you update your scripts in Git, simply commit and push your changes from your development machine:
git add your_script.sh
git commit -m "Update logic for your script"
git push
Then, on your server, pull the latest changes:
cd /opt/my-shell-scripts
git pull origin master
If you've set up an automated git pull with cron (as described above), this process can be automatic.
Best Practices
- Test Locally: Test your scripts before pushing to main/trunk branches.
- Atomic Deployments: Consider using a staging area or a symlink swap for critical scripts.
- Access Control: Limit repository access, especially if scripts contain sensitive information.
- Documentation: Maintain a
README.md
with script usage and setup instructions. - Notifications: Use email or Slack integration for cron error reporting for critical jobs.
Conclusion
By combining Git for version control and cron for scheduling, you create a robust, maintainable workflow for shell script management. This approach streamlines updates, ensures accountability, and provides a reliable automation backbone for your systems. Make this simple yet powerful combination part of your server administration toolkit today!
Further Reading:
Happy scripting!