DEV Community

Vlythr
Vlythr

Posted on

Automated system clean up - Linux

This is a tutorial on how to create a script to clean Linux system cache and useless files, and then schedule it to run on a daily basis using cron jobs.

Step 1: Creating the Cleaning Script
First, let's create a script that will perform the cleaning operations. We'll name it clean.sh. Follow these steps:

1.1 Open a text editor on your Linux system.

1.2 Start a new file and add the following lines to the script:

   #!/bin/bash

   # Clean system cache
   sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

   # Clean temporary files
   sudo rm -rf /tmp/*
Enter fullscreen mode Exit fullscreen mode

In this script, we're using the sync command followed by echo 3 to clear the system cache. Then, we use the rm command with the -rf option to remove all files and directories inside the /tmp directory.

1.3 Save the file as clean.sh in a directory of your choice.

1.4 Make the script executable by running the following command in the terminal:

   chmod +x clean.sh
Enter fullscreen mode Exit fullscreen mode

This command grants execute permissions to the script.

Step 2: Testing the Cleaning Script
Before scheduling the script with cron jobs, it's a good idea to test it to ensure it works as expected. Follow these steps to run the script manually:

2.1 Open a terminal.

2.2 Navigate to the directory where the clean.sh script is located using the cd command:

   cd /path/to/script/directory
Enter fullscreen mode Exit fullscreen mode

2.3 Run the script by typing its name:

   ./clean.sh
Enter fullscreen mode Exit fullscreen mode

This will execute the script and perform the cleaning operations. Make sure to provide your password when prompted for sudo commands.

2.4 Verify that the system cache is cleared and the temporary files are removed.

Step 3: Scheduling the Script with Cron Jobs
Now that we have the cleaning script ready, let's schedule it to run on a daily basis using cron jobs. Follow these steps:

3.1 Open a terminal.

3.2 Type the following command to edit the crontab file:

   crontab -e
Enter fullscreen mode Exit fullscreen mode

3.3 If prompted, choose an editor to edit the crontab file.

3.4 In the crontab file, add the following line to schedule the cleaning script to run daily at a specific time (e.g., 3 AM):

   0 3 * * * /path/to/clean.sh
Enter fullscreen mode Exit fullscreen mode

This line specifies that the script will run at 3 AM every day (0 minutes, 3 hours).

3.5 Save the crontab file and exit the editor.

Step 4: Verifying and Managing Cron Jobs
To verify that the cron job is set up correctly and manage cron jobs, you can use the following commands:

  • To list the existing cron jobs for the current user, type:
  crontab -l
Enter fullscreen mode Exit fullscreen mode
  • To remove all cron jobs for the current user, type:
  crontab -r
Enter fullscreen mode Exit fullscreen mode
  • To edit the cron jobs for the current user, type:
  crontab -e
Enter fullscreen mode Exit fullscreen mode

All done! The cleaning script will now be executed automatically at the specified time each day, ensuring that your system stays clean and optimized.

Remember to adjust the file paths and schedule timing in the script and cron job line according to your specific requirements.

Top comments (0)