#!/bin/bash
# Create the clean_runner.sh file
cat << 'EOF' > /home/actions-runner/clean_runner.sh
#!/bin/bash
# Check if any job is running
if pgrep -f "./run.sh" > /dev/null
then
echo "A job is currently running. Skipping clean up process."
exit 0
fi
# Start the clean up process
echo "Starting clean up process..."
# Clean work directory
rm -rf /home/actions-runner/_work/*
# Remove old builds and caches
rm -rf /home/actions-runner/_tool/*
# Delete old logs (older than 7 days)
find /home/actions-runner/_diag -name "*.log" -type f -mtime +7 -delete
# Clean Docker
docker system prune -af
# Clean npm cache
npm cache clean --force
# Clean pip cache
pip cache purge
# Remove unnecessary packages
apt-get autoremove -y
apt-get clean
# Clean /tmp directory
rm -rf /tmp/*
# Update runner
cd /home/actions-runner
./config.sh --check
# Restart runner service
systemctl restart actions.runner.*
# Free up disk space
fstrim -av
echo "Clean up process completed."
EOF
# Make the script executable
chmod +x /home/actions-runner/clean_runner.sh
# Create the cron job
(crontab -l 2>/dev/null; echo "0 4 * * * /home/actions-runner/clean_runner.sh >> /home/actions-runner/clean_runner.log 2>&1") | crontab -
echo "Cron job has been created to run the clean up script at 4 AM daily."
To use this script:
-
Save this script to a file, for example
setup_clean_cron.sh
. -
Make the script executable:
chmod +x setup_clean_cron.sh
-
Run the script with sudo privileges:
sudo ./setup_clean_cron.sh
This script will:
- Create a
clean_runner.sh
file in the/home/actions-runner/
directory containing the clean up commands. - Make
clean_runner.sh
executable. - Create a cron job to run the clean up script at 4 AM daily.
Notes:
- The clean up script will check if any job is running before performing the clean up.
- The results of the clean up process will be logged to
/home/actions-runner/clean_runner.log
. - Ensure that the user running the cron job has sufficient permissions to execute the commands in the script (especially those requiring sudo).
- You may need to adjust paths and specific commands depending on your system configuration.