fetch_releases_cronjob.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/bash
  2. # this script is intended to be run from a crontab owned by the
  3. # www-data user on ArduPilot's autotest server
  4. # CBS_REMOTES_RELOAD_TOKEN can be supplied as an environment
  5. # variable in the crontab line, for example:
  6. # 0 * * * * CBS_REMOTES_RELOAD_TOKEN=8d64ed06945 /home/custom/beta/CustomBuild/scripts/fetch_release_cronjob.sh
  7. # or can be read from base/secrets/reload_token (the token from the file gets the preference)
  8. CUSTOM_HOME=/home/custom
  9. TOPDIR=$CUSTOM_HOME/beta
  10. LOGDIR=$TOPDIR/cron
  11. BASEDIR=$TOPDIR/base
  12. # Maximum number of log files to retain
  13. MAX_LOG_FILES=50
  14. # Get the current timestamp
  15. TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
  16. # Log file name
  17. LOG_FILE="${LOGDIR}/fetch_releases_${TIMESTAMP}.log"
  18. # Function to clean up old log files
  19. cleanup_old_logs() {
  20. # Find and sort log files by modification time, oldest first
  21. LOG_FILES=($(ls -1t ${LOGDIR}/fetch_releases_*.log 2>/dev/null))
  22. # Count the number of log files
  23. NUM_LOG_FILES=${#LOG_FILES[@]}
  24. # If the number of log files is greater than the maximum allowed
  25. if [ $NUM_LOG_FILES -gt $MAX_LOG_FILES ]; then
  26. # Loop through and delete the oldest files
  27. for ((i = $MAX_LOG_FILES ; i < $NUM_LOG_FILES ; i++ )); do
  28. rm -f "${LOG_FILES[$i]}"
  29. done
  30. fi
  31. }
  32. # Call the cleanup function before executing the main script
  33. cleanup_old_logs
  34. # Call the main python script
  35. python3 $TOPDIR/CustomBuild/scripts/fetch_releases.py \
  36. --appurl https://custom-beta.ardupilot.org/refresh_remotes \
  37. --basedir $BASEDIR \
  38. >> $LOG_FILE 2>&1