moving log file into Archive directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting moving log file into Archive directory
# 1  
Old 09-16-2010
moving log file into Archive directory

Hi All,

I'm trying to write a script which will do following :

- For any old log under trace directory, if found move it to Archive
- Check for a process “process-A” if it is running abort from the script
- Else start it up (start_process-A.sh this case)
- If it fails to start the process, exit with the appropriate exit code
- All of the above steps will be logged in the log file under trace
Code:
#!/bin/ksh
SERVICE='process-A'
JOB=`basename $0`
OWNPID=$$
LOGFILE=$HOME/trace/$JOB$OWNPID.log
Archive=$HOME/trace/Archive
 
archive()
{
if [ -f $HOME/trace/ start_process-A.sh.*.log ]; then
  mv $HOME/trace/ start_process-A.sh.*.log $Archive
else
  echo "No file to archive" | tee -a ${LOGFILE}
  exit 1;
fi
}

>>>> The above function moves all the files to the Archive directory as I used wildcard (*) but I want to keep the latest file (which will be generated upon completion of this script) in the trace directory and rest in the Archive directory. I have done lots of search in the google and just cannot find it. Any help will be appreciated.
Code:
# Checking process-A .. if not found - equals to 1, start it
ps -ef | grep $SERVICE | grep -v grep
if [ $? -eq 1 ]
then
  echo "`date` : $SERVICE is down, starting up ..." | tee -a ${LOGFILE}
  $HOME/ start_process-A.sh | tee -a ${LOGFILE}
else
  echo "`date` : $SERVICE is up ..aborting from the script" | tee -a ${LOGFILE}
fi
#Moving old log file into Archive directory
archive

>> Is there a was to check the start_process-A.sh script is failed or hung? if so how do I redirect it to the log file?

Last edited by Franklin52; 09-16-2010 at 02:57 AM.. Reason: Please use code tags, thank you!
# 2  
Old 09-16-2010
For the future please use [CODE] tags around the logic. Please review the forum rules.

Let's assume(might not a good assumption) that start_process-A.sh will return a non-zero return code on error. You can check the return code just like you did with the grep. This gets more complicated when trying to check the return code from a program in the pipeline. Do you really need use tee? Can you just append(>>) to ${LOGFILE}? If you really need the output in the current script and the log file then you have to be more creative. I will wait for you response before going down that route. Do you know if the shell is ksh93?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving Files one directory to another directory shell script

Hi, Could you please assist how to move the gz files which are older than the 90 days from one folder to another folder ,before that it need to check the file system named "nfs" if size is less than 90 or not. If size is above 90 then it shouldn't perform file move and exit the script throwing... (4 Replies)
Discussion started by: venkat918
4 Replies

2. Homework & Coursework Questions

Archive and purge the log file

Hi Unix Experts, I am new in this filed. I have assignment to Archive and purge the log file using shell scripts I tried I could not get the result please help me this. Ex: test.log requirement : using shell script I need to archive the log file and nil and the content of (test.log)... (1 Reply)
Discussion started by: johney1981
1 Replies

3. UNIX for Dummies Questions & Answers

Reading the dates from a file & moving the files from a directory

Hi All, I am coding for a requirement where I need to read a file & get the values of SUB_DATE. Once the dates are found, i need to move the files based on these dates from one directory to another. ie, this is how it will be in the file, SUB_DATE = 20120608,20120607,20120606,20120606... (5 Replies)
Discussion started by: dsfreddie
5 Replies

4. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

5. Shell Programming and Scripting

Shell Script for moving 3 days old file to Archive Folder

Hi Experts, I have a "Source" folder which may contain some files. I need a shell script which should move all files which are older than 3 days to "Archive" folder. Thanks in Advance... (4 Replies)
Discussion started by: phani333
4 Replies

6. Shell Programming and Scripting

Moving files listed in a data file to a new directory using Perl

Hi, I have a data file that lists a number of files. I want to move the files named in that one to another directory. Here's what I have: #!/usr/bin/perl -w open(FILE, "<collision.txt"); my @lines=<FILE>; foreach my $lines (@lines) { system("mv $lines collisions/."); } close(FILE); ... (2 Replies)
Discussion started by: renthead720
2 Replies

7. Shell Programming and Scripting

ftp a file after checking the versions not greater then 8 in archive directory

Hi , I want to write a FTP Script which checks the No of Vesions of the files in Archive Dir and if count >= 8 Delete the oldest file from the Archive Dir and if the count is <= 8 Move the file to the Archive Dir with a CurrentDate concatenation and FTP the file to the FTP directory and send... (1 Reply)
Discussion started by: kailash.jadhav
1 Replies

8. Shell Programming and Scripting

moving file to directory in script

hi i am reading files from directory,if the files matches certain condition i need to move that file to another directory i am using if then mv $file1 > $UNIQDIR fi but i am gettin error , please help thanks Satya (4 Replies)
Discussion started by: Satyak
4 Replies

9. Shell Programming and Scripting

Moving file to directory based on condition.

Can any one help me to correct following script. I have 2 directories DropZone and ProcessZone. File pattern is *VEHDESCSUM*. Finding the 'no of files' in DropZone directory using ls *VEHDESCSUM* |wc -l If DropZone has more than one file or 0 files then exit 1 If DropZone has one file then... (2 Replies)
Discussion started by: ramanagh
2 Replies

10. UNIX for Dummies Questions & Answers

Create Year directory, date subdirectory and archive the file

Hi, After checking all the UNIX threads, I am able to come up with a solution so far. I am working on a shell script where it moves the files to a certain directory. The conditions to check are 1) Check if the file exists in the current directory. 2) Check if the destination directory... (2 Replies)
Discussion started by: madhunk
2 Replies
Login or Register to Ask a Question