This article will show you how to create cron job on RHEL 8
Use case
I want to run a cron job monthly to clean up my docker data disk as it’s eating lots of space
Located cron job folder on RHEL 8
In this case, I’ll put a bash script under /etc/cron.monthly folder
Build scripts to clean up docker data
User docker command to delete stopped/dead images forcefully.
#!/bin/bash
echo "Start cleaning docker images"
docker system prune -a -f
echo "Docker data clean up complete"
Then I save this file as “clean-docker”, the cron job configuration is done.
Test scripts to make sure you have correct syntax placed
Just run the script with bash command, I should have docker images cleaned up and custom message printed. Should be careful if script makes something important change, double check before running.
[computer@rhel8-devops cron.monthly]$ bash clean-docker
Then I have below outputs, everything looks good, the monthly running cron job configuration is done.
Start cleaning docker images
Deleted Images:
untagged: go-platform:latest
untagged: my-registry/go-platform:latest
untagged: my-registry/go-platform@sha256:b712494a7cac797edc609cec44bcfd793b0c66c0a566fdbfae1306b4be89b1fa
deleted: sha256:cf371762a4b617038072b35781e56fca1838d96e47d012265399809e97a37d67
Deleted build cache objects:
p9zz18uq4ylin8bjc2tutnmfz
sbwbqwxf2xa0l6uhn12o5p9wk
tugp9l8pff6irks4pv7ulqm90
ul3s7wdfuc3ynfguijr6fyvab
a1m2fsmq6zxsyvnkr5uvk0h82
t1q2zf4a5gxwbg5fo0uv1q120
gjt3n86m6pfdrr6xu9dj0vu62
szeplzm3vjnv86m39pyca8lxu
qau5curdmmmmdgo41ktje5r18
tiy36vr9fl4qinuzuxj7bpnoy
bh7o40ebj8yvo36qg4tylj3ir
wakp1nn0ridj5gfv5ad0q9b7d
w1o4qjgizsxh7yn5t7ajz7o87
9eh7bf1sfblidlqek4efa43hy
hlpd6nh048duc5m6ma65vxts1
Total reclaimed space: 1.198GB
Docker data clean up complete
References
- https://www.redhat.com/sysadmin/automate-linux-tasks-cron
- https://www.cyberciti.biz/faq/linux-show-what-cron-jobs-are-setup/