Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
cron script for eclean
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
acidbreez
n00b
n00b


Joined: 05 Dec 2006
Posts: 27

PostPosted: Mon Jan 06, 2025 2:37 am    Post subject: cron script for eclean Reply with quote

hey guys/gals,

I made a quick script to run as a cron job. it uses eclean to clean the dist and pkg files and then creates a log and places it into /root/.logs/cleaning/ so if you need to you can go back and review what eclean is doing

Code:
                                                 
#!/bin/bash

ls /tmp/ 2>/dev/null | while read -r files; do
        if [[ $files == 'cleaning' ]]; then
                echo "Temporary cleaning folder already exists."
                echo ''
        elif [[ $files != 'cleaning' ]]; then
                mkdir /tmp/cleaning 2>/dev/null
        fi
done

eclean-dist --deep | tee -a /tmp/cleaning/eclean-dist-$(date +%Y-%m-%d).log
eclean-pkg --deep | tee -a /tmp/cleaning/eclean-pkg$(date +%Y-%m-%d).log

#clean up tmp files
ls -lag /root/ 2>/dev/null | while read -r folders; do
        if [[ $folders != '.logs' ]]; then
                mkdir /root/.logs 2>/dev/null
                mkdir /root/.logs/cleaning 2>/dev/null
        fi
done

mv /tmp/cleaning/* /root/.logs/cleaning/


I hope this helps somebody out.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 22946

PostPosted: Mon Jan 06, 2025 3:15 am    Post subject: Reply with quote

Using ls is almost always wrong in a script, and your use here appears to be unusually wrong. There will usually be more than one entry in the directory, so your while loop will get confused and do the wrong thing in most cases. I think you just want to test whether the directory exists and, if not, create it.
Back to top
View user's profile Send private message
Banana
Moderator
Moderator


Joined: 21 May 2004
Posts: 1825
Location: Germany

PostPosted: Mon Jan 06, 2025 9:33 am    Post subject: Reply with quote

You check for a file/directory which is created by "you". Which means there is no other process or variability which could influence it. So there no need to check for this variability. It will be always the same check and the same results.
Also, why work with two different directories when in the end the files will be moved unmodified?

Here is a starting point about checking things in bash
_________________
Forum Guidelines

PFL - Portage file list - find which package a file or command belongs to.
My delta-labs.org snippets do expire
Back to top
View user's profile Send private message
acidbreez
n00b
n00b


Joined: 05 Dec 2006
Posts: 27

PostPosted: Mon Jan 06, 2025 5:16 pm    Post subject: Reply with quote

thanks guys I am fairly new to programming/scripting. i will take your suggestions in consideration and do some more reading.

EDIT:

so if I am right you guys are really saying this is all I need:

Code:
#!/bin/bash

if [ -d /root/.logs/cleaning ]; then
        eclean-dist --deep | tee -a /root/.logs/cleaning/eclean-dist-$(date +%Y-%m-%d).log
        eclean-pkg --deep | tee -a /root/.logs/cleaning/eclean-pkg$(date +%Y-%m-%d).log
fi
Back to top
View user's profile Send private message
Banana
Moderator
Moderator


Joined: 21 May 2004
Posts: 1825
Location: Germany

PostPosted: Mon Jan 06, 2025 6:01 pm    Post subject: Reply with quote

No problem.

The script you have now, will only run if the correct directory exists. If this is ok for you, then the script is fine.
_________________
Forum Guidelines

PFL - Portage file list - find which package a file or command belongs to.
My delta-labs.org snippets do expire
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3836
Location: Rasi, Finland

PostPosted: Mon Jan 06, 2025 6:10 pm    Post subject: Reply with quote

If you want to be more strict/paranoid, then
Code:
logdir='/root/.logs/cleaning'; if [ -d "$logdir" ] && [ -r "$logdir" ] && [ -x "$logdir" ] &&; then
... but since your script is meant to be ran as root, that's probably not necessary. ;)

Code:
man test
... will tell you more. ;)
_________________
..: Zucca :..

My gentoo installs:
init=/sbin/openrc-init
-systemd -logind -elogind seatd

Quote:
I am NaN! I am a man!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum