Giter Site home page Giter Site logo

prodbygodfather / crontab-auto-dir-delete Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 12 KB

Delete a folder or directory of a program at a specified time using crontab with all access levels even in the running mode of software and directory files in ubuntu linux

License: MIT License

Shell 100.00%
bash-script crontab crontab-format crontab-task

crontab-auto-dir-delete's Introduction

crontab auto dir delete

One of the challenges created in the Ubuntu environment was that we wanted our project directory to be deleted after 10 days of use and after expiration so that the client would receive the final and paid version of the project. This led us to turn to crontab within the Ubuntu environment.

structure

A .sh file with name delete.sh is included for this project, which is placed in this repository. In the first step, we get the time from the Internet to make sure that the time is not manipulated by the user on the computer. This is how we got the seconds of the year from a website:

ntp_server="http://worldtimeapi.org/api/ip"

# Get the current timestamp (in seconds) from the NTP server
timestamp=$(curl -s "$ntp_server" | jq -r '.unixtime')

We considered all the possibilities and gave the possibility of the internet being down, so now we have to get the current time from the system as a second way if there is no internet.

if [ -z "$timestamp" ]; then
    echo "Failed to fetch time from the internet. Using system time."
    timestamp=$(date +%s)
fi

In the next part, we should have given the desired time to delete the project to the file, so that if the current time exceeds the time we gave, the project will be deleted.

We put the target time into the target_time variable like this And with the format YYYY-MM-DD HH:MM:SS we gave:

target_datetime="2024-04-17 13:35:00"

And to check the current time with the target time, we had to convert the target time to seconds:

target_timestamp=$(date -d "$target_datetime" +%s)

Now we checked if the current time has reached the target time or if it has passed the target time. If it passed, delete our folder.

time_diff=$((target_timestamp - timestamp))

# Check if the target time has passed
if [ $time_diff -le 0 ]; then
    # If the target time has passed, delete the folder
    rm -rf /your/directory/source
    echo "Folder deleted successfully!"
else
    # If the target time has not passed yet, calculate the remaining time in seconds
    echo "Folder will be deleted on $target_datetime"
    echo "Remaining time: $(date -u -d @$time_diff +'%H:%M:%S')"
fi

requirements

This file uses jq package. Type the following command to install this package:

sudo apt-get install jq

Basically, this package in this project has the task of getting a key from the dictionary.

crontab

The first reason for choosing crontab was that it does its work again after the system is restarted or in all different situations, and this reason is completely accepted. Here we have to give the path of our delete.sh file to crontab so that our file is executed every time and the desired checks are done.

  1. In the first step, we run crontab with the following command in the terminal:
    crontab -e
    If you are asked a question, choose number 1.
  2. Replace the following code in the last line of the file that opens.
    * * * * * /bin/bash /the/sh/file/location/delete.sh
    The story of the first 5 stars of this order is as follows.
        * * * * * /path/to/command
        - - - - -
        | | | | |
        | | | | +---- Day of the week (0 - 7) (Sunday=0 or 7)
        | | | +------ Month (1 - 12)
        | | +-------- Day of the month (1 - 31)
        | +---------- Hour (0 - 23)
        +------------ Minute (0 - 59)
    
    We entered all 5 stars here without numbers so that our file will run every minute, but you can edit this for yourself according to the guide above.

In this section, our work with crontab is finished.

sudo

One of our challenges in the next section is that naturally, when the files of a folder or directory are running, we cannot delete the folder unless we have sudo access. However, crontab is not allowed to use sudo. To solve this challenge, these steps must be taken.

  1. Go to visudo with the following command in the terminal:
    sudo visudo
  2. To have sudo access to delete the desired folder without giving this permission in the terminal, add this code at the end of the opened file: (Change your directory path in the last section)
    username ALL=(ALL) NOPASSWD: /bin/rm -rf /your/directory/source

By doing these steps, your folder will be deleted in the target time (the probability of deletion is one minute more).

crontab-auto-dir-delete's People

Contributors

prodbygodfather avatar abarvision avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.