Skip to content

Instantly share code, notes, and snippets.

@lucabusin
Last active February 5, 2021 22:07
Show Gist options
  • Save lucabusin/cad36a45764f2fc2e2daa81e2db4186d to your computer and use it in GitHub Desktop.
Save lucabusin/cad36a45764f2fc2e2daa81e2db4186d to your computer and use it in GitHub Desktop.
Shell script to purge Rundeck execution history (extended from @unicolet's script https://gist.github.com/unicolet/af648a97163ce6b44645)
#!/bin/sh
# keep last few executions for each job
KEEP=100
# db connection params
HOST=YOURDBHOST
PORT=YOURDBPORT
USERNAME=YOURDBUSERNAME
PASSWORD=YOURDBPASSWORD
DB=YOURDB
cd /var/lib/rundeck/logs/rundeck
JOBS=`find . -maxdepth 3 -path "*/job/*" -type d`
for j in $JOBS ; do
echo "Processing job $j"
ids=`find $j -iname "*.rdlog" | sed -e "s/.*\/\([0-9]*\)\.rdlog/\1/" | sort -n -r`
declare -a JOBIDS=($ids)
if [ ${#JOBIDS[@]} -gt $KEEP ]; then
for job in ${JOBIDS[@]:$KEEP};do
echo " * Deleting job: $job"
echo " rm -rf $j/logs/$job.*"
rm -rf $j/logs/$job.*
workflowid=`mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -s -N -e "select workflow_id from execution where id=$job" $DB`
workflowstepids=`mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -s -N -e "select workflow_step_id from workflow_workflow_step where workflow_commands_id=$workflowid" $DB`
declare -a WSIDS=($workflowstepids)
for workflowstepid in $WSIDS ; do
echo " mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e 'delete from workflow_workflow_step where workflow_step_id=$workflowstepid' $DB"
mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e "delete from workflow_workflow_step where workflow_step_id=$workflowstepid" $DB
echo " mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e 'delete from workflow_step where id=$workflowstepid' $DB"
mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e "delete from workflow_step where id=$workflowstepid" $DB
done
echo " mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e 'delete from execution where id=$job' $DB"
mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e "delete from execution where id=$job" $DB
echo " mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e 'delete from base_report where jc_exec_id=$job' $DB"
mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e "delete from base_report where jc_exec_id=$job" $DB
echo " mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e 'delete from workflow where id=$workflowid' $DB"
mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e "delete from workflow where id=$workflowid" $DB
done
fi
done
@lucabusin
Copy link
Author

In addition to deleting log files, executions and base_reports, this script deletes workflow and workflow steps generated by an execution.
I have also moved db connection params as variables to avoid repetition.
Finally in my case I needed it to use mysql instead of psql.

@Krishnababu505
Copy link

Hello Luca,

Thanks for the script it really helps me, when i am using the same script it shows deleting jobs but it is keep on executing same steps it's not going to further.

echo " * Deleting job: $job"

         rm -rf $j/logs/$job.*

Deleting job: 3104429
14:48:45 | | | rm -rf ./XYZ/job/81476133-1da6-40fc-7d3c-abf2caedd1f9/logs/3104429.*

Later it is failing with below timeout error after 2hr

15:29:20 | | | Result: 2147483647

15:29:21 | | | Failed: NonZeroResultCode: Result code was 2147483647
| ANY04210451 [console] |
15:29:21 | | | Cancellation while running step [1]

could you please help me what causes this issue. is this caused due to any permission issue ??

Thanks In Advance.
Regards,
Krishna

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment