Giter Club home page Giter Club logo

create_delete_bash's Introduction

Script to create empty folders

1) Create a directory to perform the operation and go to it

sudo mkdir folder_script
cd folder_script/ 

2) We create a file with the extension “.sh” so that the shell understands that this will be a script

touch folder.sh

3) Add execute permissions for this file

sudo chmod +x folder.sh  

4) Edit the file and add the following

sudo nano folder.sh 
#!/bin/bash  

folders_count=10

path_to_save=/home/ubuntu/folder_script/


current_date=$(date +%Y.%m.%d)


for ((i=1;i<=$folders_count;i++)); do
 
 folder_name="${current_date}_Папка$i"

  mkdir -p "$path_to_save/$folder_name"
done

echo "Script finished in $(date)" >> $path_to_save/script.log

5) Running a script

./folder.sh 

Executing a script via CRON

1) Command to edit cron schedule

crontab -e 

2) Adding a script to the end of the file that will be executed at 0 minutes , 1 hour, * - every day, * - every month, * - every day of the week.

0 1 * * * /bin/bash /home/ubuntu/folder_script/folder.sh 

3) Setting the timer to 3 minutes (optional)

(*/3 * * * * /bin/bash /home/ubuntu/folder_script/folder.sh ) 

4) View a list of added tasks

crontab -l

Adding a timer to systemd to execute a script, for example, once every 30 seconds.

1) creating and editing a timer configuration file

sudo nano /etc/systemd/system/folder_script.timer 
[Unit]
Description=Folder Script Timer

[Timer]  
OnUnitActiveSec=30s 
Unit=folder_script.service  

[Install]  
WantedBy=timers.target  

2) creating and editing a timer configuration file

sudo nano /etc/systemd/system/folder_script.service
[Unit]
Description=Folder Script Service 

[Service]  
Type=simple  
ExecStart=/bin/bash /home/ubuntu/folder_script/folder.sh 

[Install]
WantedBy=multi-user.target 

reloading systemd config files

sudo systemctl daemon-reload 

command to run in auto mode at system startup

sudo systemctl enable folder_script.timer

immediate start of the timer (one shot for example as a single launch)

sudo systemctl start folder_script.timer 

display logs of the script run

journalctl -u folder_script.service

a list of systemd timers as well as information about scheduled subsequent runs

systemctl list-timers 

Creating a script to delete empty folders in a specified directory and then running it through systemd

1) Create and change directory

mkdir del_script/
cd del_script/

2) Add the following content to our file

nano del.sh
#!/bin/bash

path_to_dir=/home/ubuntu/folder_script


find "$path_to_dir" -type d -empty -delete


echo "The script ran successfully $(date), the directory $path_to_dir cleared of empty folders" >>/home/ubuntu/del_script/del.log

3) Making our file executable

chmod +x del.sh

4) Editing del.timer and del.service files adding the following content

nano /etc/systemd/system/del.timer
[Unit]
Description=Delete Script Timer

[Timer]
OnUnitActiveSec=40s
Unit=del.service

[Install]
WantedBy=timers.target
nano /etc/systemd/system/del.service
[Unit]
Description=Delete Script Service

[Service]
Type=simple
ExecStart=/bin/bash /home/ubuntu/del_script/del.sh

[Install]
WantedBy=multi-user.target

Turn on the del.timer timer to automatically run the script

systemctl enable del.timer

create_delete_bash's People

Contributors

pashashredder avatar

Watchers

 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.