Create a Cron to Run a Script in a Time Interval Linux

Dec 21, 2023

Related to: Linux - SysAdmin - DevOps

Go to Your Desired Cron Folder

You can choose the folders based on their names (self-explanatory):

  • /etc/cron.hourly
  • /etc/cron.daily
  • /etc/cron.weekly
  • /etc/cron.monthly
cd /etc/cron.weekly

Create The Script File

We just need to create a file with a descriptive name:

Let’s create a docker prune example (more info on Docker Maintenance CheatSheet and Tips).

sudo vim docker-prune
#!/bin/bash
docker system prune -f

Set the Permissions

sudo chmod +x docker-prune

Test it

This will run all your weekly scripts.

sudo run-parts /etc/cron.weekly

Graph View