DEV Community

Cover image for Automating Routine Tasks with Cron on Mac and Linux
George K.
George K.

Posted on

Automating Routine Tasks with Cron on Mac and Linux

Automating routine tasks is something we start doing once and never look back.

For example, I work with many node projects during the day running them locally and each require node_modules. In the end of the day I like to delete all the modules because I might not need them till next time I need them. To save space on my hard drive I run

find . -name node_modules -exec rm -rf {} \;
Enter fullscreen mode Exit fullscreen mode

from the work folder and it goes through all the nested folders looking for and removing node_modules folders. Every time it's several GBs of freed up space.

Surely it can be placed into the .bash_profile with an alias to make it quicker:

alias rmnm='find . -name node_modules -exec rm -rf {} \;'
Enter fullscreen mode Exit fullscreen mode

With that I can just run rmnm command which is my custom alias meaning "remove node modules" and now we can save time by typing only rmnm from any folder and it will recursively delete all the node_modules folders found.

But... It's still manual labor.

With cron we can automate such tasks, let's see how.

Unleashing the Power of Cron Jobs

Cron jobs are like silent assistants working in the background, executing tasks based on a predefined schedule. They can perform anything from simple scripts to complex operations, making them an invaluable tool for system management.

Getting Started with Cron

To check your existing cron jobs, open your terminal and type:

crontab -l
Enter fullscreen mode Exit fullscreen mode

This will list all the scheduled tasks associated with your user.

Edit Your Cron Jobs

To add or modify cron jobs, type:

crontab -e
Enter fullscreen mode Exit fullscreen mode

This opens the crontab file for editing, where you can unleash the magic of automation.

Anatomy of a Cron Job

A cron job entry follows this pattern:

* * * * * command_to_be_executed
Enter fullscreen mode Exit fullscreen mode

The asterisks represent minute, hour, day of the month, month, and day of the week. You can customize these values or use special characters to define the schedule.

Example: automating removal of node_modules

  1. Open crontab by doing
crontab -e
Enter fullscreen mode Exit fullscreen mode
  1. Schedule the removal command to run daily at let's say 6pm inside the folder code_projects. We will define the schedule pattern as 0 18 * * * which mean at 0 minutes at 18 hours every day and we will go into the folder with all the coding projects, let's say /Users/mike/code_projects (absolute path) and run the removal command:
0 18 * * * cd /Users/mike/code_projects && find . -name node_modules -exec rm -rf {} \;
Enter fullscreen mode Exit fullscreen mode

Now every day at 18:00 it will run and delete all the node_modules within any nested folders of code_projects.

Another example if we want to clean pm2 logs on our Ubuntu server which with time eat up a lot of space:

0 0 * * * pm2 flush
Enter fullscreen mode Exit fullscreen mode

Now every midnight the logs will be removed.

We can also run Node file easily. For example, our app writes files inside the project/server/output folder and we want to delete files older than 10 days.

  1. Node.js Script (deleteOldFiles.js) which for example sits in the server's root folder project/server:
const fs = require('fs');
const path = require('path');

const outputFolderPath = path.join(__dirname, 'output');
const daysThreshold = 10 * 24 * 60 * 60 * 1000; // 10 days in milliseconds

fs.readdir(outputFolderPath, (err, files) => {
  if (err) {
    console.error('Error reading directory:', err);
    return;
  }

  const currentDate = new Date();

  files.forEach((file) => {
    const filePath = path.join(outputFolderPath, file);
    const stats = fs.statSync(filePath);
    const fileAge = currentDate - stats.mtime;

    if (fileAge > daysThreshold) {
      fs.unlink(filePath, (unlinkErr) => {
        if (unlinkErr) {
          console.error('Error deleting file:', unlinkErr);
        } else {
          console.log(`Deleted file: ${file}`);
        }
      });
    }
  });
});
Enter fullscreen mode Exit fullscreen mode
  1. Make it executable:
chmod +x deleteOldFiles.js
Enter fullscreen mode Exit fullscreen mode
  1. Cron Job: Open your crontab:
crontab -e
Enter fullscreen mode Exit fullscreen mode
  1. Add the following line to run the script daily at midnight:
0 0 * * * /path/to/your/project/server/deleteOldFiles.js
Enter fullscreen mode Exit fullscreen mode

| This path has to be absolute, to find it out run pwd from you server folder.

Save and exit.

Once you've edited your crontab, you're all set. Cron will take care of the rest, executing your scheduled tasks at the specified intervals.

Pretty handy, isn't it?

Top comments (0)