Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script
# 1  
Old 12-09-2014
Shell Script

I am trying to delete the log files on server..I have made code but it is not working.

Code:
#!/bin/bash

#moofwd-rt.log check
CHETAN='/Users/ajit/'

if [ -a $CHETAN/chetan.log ];
  then
  FILE1=$(du -ks $CHETAN/chetan.log | cut -c1)
  if [ $FILE1 -gt 0 ];
    then
    echo "File size has exceeded 1 GB" | mail -s "chetan.log" chetanp@moofwd.com  -c cchetans@moofwd.com
elif [ $FILE1 -gt 1073741 ];
then
rm -rf $CHETAN/chetan.log
  fi
else
 echo "chetan.log not found"
fi

But it is not working...I need help

---------- Post updated at 11:47 AM ---------- Previous update was at 11:44 AM ----------

Heyy Plz helppp meeee to create script

---------- Post updated at 11:57 AM ---------- Previous update was at 11:47 AM ----------

Anyone heree??????
Moderator's Comments:
Mod Comment Please re-read the rules you agreed to when you opened your account on this forum.

Use CODE tags when you post sample input, sample output, and code segments.

Do not expect volunteers here to immediately solve all of your problems. Some of us have day jobs. Some of us like to sleep at night. Some of us have other things to do besides act as your personal problem solver. Demanding help twice in less than an hour after your initial post is a sure fire way to annoy volunteers who might be interested in helping you.

Instead of showing us a script and saying it doesn't work, tell us what the script is supposed to do, tell us what the script is doing (including showing us the input data it is processing), and tell us what the script is doing on your system showing us any output (standard output, files or mail messages created, and diagnostic messages produced), and show us the output you were hoping to get.

You have shown us that you're using bash as your shell. (Thank you.) It frequently also helps to know what OS you're using. Options and limits vary from system to system; if we know what system you're using, we have a much better chance of giving you advice that will work correctly in your environment.

Last edited by Don Cragun; 12-09-2014 at 03:02 AM.. Reason: Add CODE tags, add Moderator's Note.
# 2  
Old 12-09-2014
Be patient bro, this is not a chat room Smilie
I think you want something like this (untested)
Code:
#!/bin/bash

#moofwd-rt.log check
CHETAN='/Users/ajit'

if [ -a $CHETAN/chetan.log ];
then # file exists
    FILE1=$(du -ks $CHETAN/chetan.log | awk '{print $1}') # get file size
    if [ $FILE1 -gt 1073741 ]; # file greater than ~1 GB
    then # send mail + delete file
    echo "File size has exceeded 1 GB" | mail -s "chetan.log" chetanp@moofwd.com -c cchetans@moofwd.com
    rm -rf $CHETAN/chetan.log
    else 
    echo "File size is under 1 GB"
    fi
else # file does not exist
    echo "chetan.log not found"
fi

This User Gave Thanks to junior-helper For This Post:
# 3  
Old 12-09-2014
Thanks Smilie

I want to make script like this

1. I need to check chatan.log file size
2. If it is greater then 1 GB then send me mail
3. If it is grater then 2 GB or Equal to 2 b then it need to delete automatically

Can you please help me?

---------- Post updated at 01:24 PM ---------- Previous update was at 12:36 PM ----------

Hello Plz help mee...i
# 4  
Old 12-09-2014
Try this and let me know if it does what it should do
Code:
#!/bin/bash

#moofwd-rt.log check
CHETAN='/Users/ajit'

if [ -a $CHETAN/chetan.log ];
then # file exists
    FILE1=$(du -ks $CHETAN/chetan.log | awk '{print $1}') # get file size in K

    if [ $FILE1 -ge 2097152 ]; # file size is greater than or equal to 2 GB
    then # delete
        echo "File size >= 2 GB, deleting file"
        rm -rf $CHETAN/chetan.log
    elif [ $FILE1 -gt 1048576 ]; # file size is greater than 1 GB
    then # send mail
        echo "File size has exceeded 1 GB" | mail -s "chetan.log" chetanp@moofwd.com -c cchetans@moofwd.com
    else # file size did not reach 1 GB
        echo "File size is under 1 GB"
    fi

else # file does not exist
    echo "chetan.log not found"
fi

# 5  
Old 12-09-2014
First up all Thank you.... I tried this



Code:
#!/bin/bash

#moofwd-rt.log check
CHETAN='/Users/ajit'

if [ -a $CHETAN/chetan.log ];
then # file exists
    FILE1=$(du -ks $CHETAN/chetan.log | awk '{print $1}') # get file size in K

    if [ $FILE1 -ge 2097152 ]; # file size is greater than or equal to 2 GB
    then # delete
        echo "File size >= 2 GB, deleting file"
        rm -rf $CHETAN/chetan.log
    elif [ $FILE1 -gt 1048576 ]; # file size is greater than 1 GB
    then # send mail
        echo "File size has exceeded 1 GB" | mail -s "chetan.log" chetanp@moofwd.com -c cchetans@moofwd.com
   # else # file size did not reach 1 GB
   # echo "File size is under 1 GB"
    fi

else # file does not exist
    echo "chetan.log not found"
fi



But no luck


I only want

1. Files size 1gb >> Send Email Notification
2. Files Size grater than 2 GB >> DELTE FIles
3. file does not exist

I only want this I disabled the above code as

# else # file size did not reach 1 GB
# echo "File size is under 1 GB"

I do not need this...Also tried with above code but e-mails notification are not getting...

Tahnk you in Advancee...plzzz

Last edited by Don Cragun; 12-09-2014 at 04:34 AM.. Reason: Please use CODE tags!
# 6  
Old 12-09-2014
You haven't said what OS you're using. On some systems test -a file tests for the existence of any file named file; on others it is a binary and operator between two expressions. It would be safer to use test -e file to test for the existence of a file named file or test -f file to test whether or not file exists and is a regular file.

Removing a file won't necessarily free up any space on your file system. If there is a process that is keeping the log file open while writing to it over an extended period of time, the process writing to the log may continue writing to the old file that no longer has a name in the file system. (So, any messages written to the log will be lost forever. And, if you create a new log file with the same name, it may remain empty while messages continue to be written to the old, inaccessible file.) It would be safer to copy /dev/null to the log than to remove it if you want to free up space that had been allocated. But, you really need to be sure that any process that had the log open closes the log file before you remove or truncate it to be sure that future messages written to the log will not be written at a starting offset of 2+Gb.

Your file size calculations are assuming a certain filesystem type (such as with direct blocks, indirect blocks, and double indirect blocks). You might want to key off of the file size (from the stat or ls utilities) rather than the number of blocks allocated (from the du utility). The code junior-helper suggested assumes that all blocks reported by du contain data (i.e., there are no indirect blocks allocated). Whether this does what you want is again filesystem dependent.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 12-09-2014
Quote:
Also tried with above code but e-mails notification are not getting...
Is the current file size greater than 1 GB Smilie Maybe the current file size is less than 1 GB right now...

Run this command on the command line (not in script!) and see if it will work:
Code:
echo "File size has exceeded 1 GB" | mail -s "chetan.log" chetanp@moofwd.com -c cchetans@moofwd.com

This User Gave Thanks to junior-helper For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script newbie- how to generate service log from shell script

Hi, I am totally a newbie to any programming languages and I just started an entry level job in an IT company. One of my recent tasks is to create a script that is able to show the log file of linux service (i.e. ntpd service) lets say, if I run my script ./test.sh, the output should be... (3 Replies)
Discussion started by: xiaogeji
3 Replies

2. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

3. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

4. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

5. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

6. Shell Programming and Scripting

Correct shell script to Call One shell script from another shell script

Hi All, I have new for shell scripting. Problem : I have one scrip at serv1 and path of server is /apps/dev/provimage/scripts and script name:extract_ancillary.bat. I need to call this script at server2(my working server) and execute at server2 . Please let me know how to build the... (5 Replies)
Discussion started by: Vineeta Nigam
5 Replies

7. Shell Programming and Scripting

call another shell script and pass parameters to that shell script

Hi, I basically have 2 shell scripts. One is a shell script will get the variable value from the user. The variable is nothing but the IP of the remote system. Another shell script is a script that does the job of connecting to the remote system using ssh. This uses a expect utility in turn. ... (2 Replies)
Discussion started by: sunrexstar
2 Replies

8. Shell Programming and Scripting

How to use ssh execute other shell script on other host (shell script include nohup)?

i want use ssh on the host01 to execute autoexec.sh on the host02 like following : host01> ssh host02 autoexec.sh autoexec.sh include nohup command like follwing : nohup /home/jack/deletedata.sh & after i execute ssh host02 autoexec.sh one the host01. i can't found deletedata.sh... (1 Reply)
Discussion started by: orablue
1 Replies

9. Shell Programming and Scripting

invoking a shell script inside cgi shell script

Hi, I have an HTML form through which I get some text as input. i need to run a shell script say script.sh inside a perl-cgi script named main_cgi.sh on the form input. I want to write the contents of the form in a file and then perform some command line operations like grep, cat on the text... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies

10. Shell Programming and Scripting

How to Run a shell script from Perl script in Parent shell?

Hi Perl/UNIX experts, I have a problem in running a shell script from my perl script (auto.pl). I run the perl script using perl auto.pl from the shell prompt The shell script picks the files in "input" folder and procesess it. The shell script blue.sh has this code. export... (16 Replies)
Discussion started by: hifake
16 Replies
Login or Register to Ask a Question