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
sudo chmod +x folder.sh
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
./folder.sh
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 * * * * /bin/bash /home/ubuntu/folder_script/folder.sh )
crontab -l
sudo nano /etc/systemd/system/folder_script.timer
[Unit]
Description=Folder Script Timer
[Timer]
OnUnitActiveSec=30s
Unit=folder_script.service
[Install]
WantedBy=timers.target
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
sudo systemctl daemon-reload
sudo systemctl enable folder_script.timer
sudo systemctl start folder_script.timer
journalctl -u folder_script.service
systemctl list-timers
Creating a script to delete empty folders in a specified directory and then running it through systemd
mkdir del_script/
cd del_script/
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
chmod +x del.sh
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
systemctl enable del.timer