Operating System - HP-UX
1830938 Members
2026 Online
110017 Solutions
New Discussion

Hi, need assistance with script again ...Please reply...

 
SOLVED
Go to solution
Shikha Punyani
Advisor

Hi, need assistance with script again ...Please reply...

I am not good at Unix and I always got help from this website :)
we want to create a script on Hp Unix server in which
Description:
There is a Filesystem with name:
/home/maestro
It should remove some subdirectories in it which I will explain below, when Filesystem size....reaches 80%
the directories under this filesystem are :
/home/maestro/stdlist/2011.05.29
/home/maestro/stdlist/logs
/home/maestro/stdlist/traces
It should delete all the directories starting from 2011* except last two days
and It should delete all the files under Logs and Traces except last two days...


Please suggest something on the same :(
14 REPLIES 14
Patrick Wallek
Honored Contributor
Solution

Re: Hi, need assistance with script again ...Please reply...

You need to use the 'find' command.

Something like this should work:

find /home/maestro/stdlist -type d -name "2011*" -mtime +2 -exec rm -rf {} +

find /home/maestro/stdlist/logs /home/maestro/stdlist/traces -type f -mtime +2 -exec rm -f {} +

Shikha Punyani
Advisor

Re: Hi, need assistance with script again ...Please reply...

How to satisfy this condition in script that it should take actions only if the File system is greater then 80 %?
g3jza
Esteemed Contributor

Re: Hi, need assistance with script again ...Please reply...


for example:

#!/bin/sh

a=`bdf | grep /home/maestro | awk '{print $5}'|cut -d % -f1`

if [ $a -gt 80 ]
then find /home/maestro/stdlist -type d -name "2011*" -mtime +2 -exec rm -rf {} +
fi

g3jza
Esteemed Contributor

Re: Hi, need assistance with script again ...Please reply...

to do also the second "find" :

#!/bin/sh

a=`bdf | grep /home/maestro | awk '{print $5}'|cut -d % -f1`

if [ $a -gt 80 ]
then find /home/maestro/stdlist -type d -name "2011*" -mtime +2 -exec rm -rf {} + && find /home/maestro/stdlist/logs /home/maestro/stdlist/traces -type f -mtime +2 -exec rm -f {} +
fi
Shikha Punyani
Advisor

Re: Hi, need assistance with script again ...Please reply...

Thanks you guys..you all are great...I will try creating script now and will get bak to you with results by tommorow:-)
You guys made my day :-)
點燃
Valued Contributor

Re: Hi, need assistance with script again ...Please reply...

If you really think they made your day, you may thank them by assigning some points?

cheers :D
Man's mind, once stretched by a new idea, never regains its original dimensions
James R. Ferguson
Acclaimed Contributor

Re: Hi, need assistance with script again ...Please reply...

Hi:

Instead of creating a second and third process to finish the work the first process could easily complete, as in:

# a=`bdf | grep /home/maestro | awk '{print $5}'|cut -d % -f1`

...you could do:

# a=$(bdf | awk '/\/home\/maestro/ {split($5,ary,"%");print ary[1]})

This eliminates the 'grep' and the 'cut' processes and lets 'awk' match and parse everything you want.

Note the $( ... ) syntax instead of the archaic back-ticks too.

Regards!

...JRF...


Shikha Punyani
Advisor

Re: Hi, need assistance with script again ...Please reply...

Hey wow it is kewl :-)
All in one line....
I will try this as well....

Thanks a ton !
James R. Ferguson
Acclaimed Contributor

Re: Hi, need assistance with script again ...Please reply...

Hi (again):

> Thanks a ton !

If you re happy with the answers you received, please see the following about assigning points:


Regards!

...JRF...

Shikha Punyani
Advisor

Re: Hi, need assistance with script again ...Please reply...

sure why not.. I will be more then happy to do that :)
I gave 10 to everyone :) :) :)
Dennis Handly
Acclaimed Contributor

Re: Hi, need assistance with script again ...Please reply...

One thing you need to be careful about the bdf output is that sometimes the lines are split into two if the mount point is long.
http://h30499.www3.hp.com/t5/Languages-and-Scripting/script-to-alert-when-File-system-has-reached-gt-87/m-p/5265424#M41339

Shikha Punyani
Advisor

Re: Hi, need assistance with script again ...Please reply...

Hi All,
I am facing one more challenge in this script..I wanted to keep it simple which Ican understand so I have made a big however easy script..

#!/bin/sh
a=`bdf | grep /home/maestro | awk '{print $5}'|cut -d % -f1`

if [ $a -gt 80 ]
then
cd /home/maestro/stdlist
rm -r 2011*
cd /home/maestro/stdlist/logs
rm "2011*
cd /home/maestro/stdlist/traces
rm 2011*
cd /home/maestro/methods/traces
rm *.log
cd /home/maestro | grep "core"
b=$?
if [$b -e 0 ]
then
rm core
else
exit
fi
fi

Now we will be running this script via Maestro..m not sure if anyone aware of this scheduling tool or not? We will not be running it via cron job or something..
Now rght now we are doingthis server manually on the server.. what we do is we login to the server we our ID's .. and then to delete these files we use sudo command for example
sudo rm -r 2011*
Then it ask for a password and then it will remove those files....
Is it possible for the above script to run the commands automatically as sudo?
Please suggest....
Dennis Handly
Acclaimed Contributor

Re: Hi, need assistance with script again ...Please reply...

>cd /home/maestro/stdlist

You may want to combine the rms:
rm -rf 2011* logs/2011* traces/2011* ../methods/traces/*.log
(Didn't you want to do that find(1) and keep the last two days?)

>cd /home/maestro | grep "core"

Don't you mean:
cd /home/maestro; ls | grep -q core

>if [ $b -e 0 ]; then

There is no reason for this complexity. Just do:
rm -f core

>Is it possible for the above script to run the commands automatically as sudo?

Yes. You can set up sudo to not have a password and only allow specific users to run a specific script.
Shikha Punyani
Advisor

Re: Hi, need assistance with script again ...Please reply...

Thank you :)