LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2013, 07:18 AM   #1
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
cronscript on Solaris 10


Hi,

We are using Solaris.

I have written a shell script which zip the log files of current date and move them into the history directory every morning in non-production hours.

Here is the script:

Code:
#!/bin/sh
#
# Remove excess trace files from the current directory
#
# If a process of the same name and PID as the extension
# of the trace file is executing, then keep that file
# otherwise remove it
#
#set -x

HISTORY_DIR=history

echo "Archiving Start Date/Time " `date`
echo "Archiving  redundant das log files files" `date +%Y-%m-%d`

cd /export/home/das/current-das/das/log

if [ ! -d ${HISTORY_DIR} ]
then
    mkdir ${HISTORY_DIR}
    if [ $? -ne 0 ]
    then
        echo "\aUnable to create ${HISTORY_DIR} directory. No history will be saved"
    else
        HISTORY=1
    fi
else
    HISTORY=1
fi

gzip *.log.2013*
echo "All files archived to gz"
echo "Moving files to history folder"
mv *.gz ${HISTORY_DIR}
echo "All Files Moved"
echo "Archiving End Date/Time" `date`
exit 0
But the problem is, suppose we deploy new release of the application. Then, this script is already running at it's scheduled time but it doesn't create history directory in it's specified location in the new release directory and also do not create the cronScript.log named file for all the daily log entries.

Here is the cronjob scheduled time:
Quote:
0 6 * * * /export/home/das/cronScript/z_Cleanup_das_log.sh >> /export/home/das/current-das/das/log/history/cronScript.log 2>&1
Please help me in this issue.

Last edited by Satyaveer Arya; 08-17-2013 at 11:29 AM.
 
Old 08-12-2013, 07:58 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by Satyaveer Arya View Post
But the problem is, suppose we deploy new release of the application. Then, this script is already running at it's scheduled time but it doesn't create history directory in it's specified location in the new release directory and also do not create the cronScript.log named file for all the daily log entries.
I'm not sure I understand the actual problem:

1) If you deploy a new application version around the time this cronjob runs: Disable the cronjob until the deployment is done, then reactivate the cron job again. Next time around all should be OK.

2) How do you deploy a new application? The above script seems to have a fixed directory it works in (/export/home/das/current-das/das/log).
- Are new deployments copied over the current one?
- Do new versions have a version number in their dir structure?

One way, which would make your script work and a possible roll-back possible would be to use a symbolic link:
Code:
# example current situation
/export/home/das/das-1.2.3/
/export/home/das/das-1.2.4/
/export/home/das/current-das/ -> links to /export/home/das/das-1.2.4/

# new version arrives:
/export/home/das/das-1.2.3/
/export/home/das/das-1.2.4/
/export/home/das/das-1.3.0/
/export/home/das/current-das/ -> links to /export/home/das/das-1.3.0/
The only thing you need to do when a new version arrives is change the link. If the new version is faulty in any way, change the link back. This should also work for the fixed directory structure that is present in your script.

But like I said before: Not sure if I understand the problem.
 
Old 08-12-2013, 08:05 AM   #3
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
How do you deploy a new application? The above script seems to have a fixed directory it works in (/export/home/das/current-das/das/log).
- Are new deployments copied over the current one?
- Do new versions have a version number in their dir structure?
New application is deployed separately, separate directory structures are made in their respective directories.

No, new deployments are not copied over the current one.

Yes, new versions have a version number and for that we do create softlink in home directory.
 
Old 08-12-2013, 08:19 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by Satyaveer Arya
But the problem is, suppose we deploy new release of the application. Then, this script is already running at it's scheduled time but it doesn't create history directory in it's specified location in the new release directory and also do not create the cronScript.log named file for all the daily log entries.
If I read the above correctly then you are deploying while this script is running; If that is the case then you might run into problems (see point 1 in post #2).

If the deployment was done before or after the script runs then something else must be wrong. First thing that comes to mind are permissions on files and/or directories. Easy to find out: check the log files and/or mails received from cron.

If I'm still off course then please elaborate on your problem.
 
Old 08-12-2013, 09:20 AM   #5
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Ok, If we leave this topic for a while, can you please tell me how would I create current month's directory automatically every month, in history directory.
Like this - 2013-08.
For directory structure of history directory, you can check above script.
 
Old 08-12-2013, 09:30 AM   #6
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
I think this is correct but with some flaws:
Code:
#!/bin/sh
#
# Remove excess trace files from the current directory
#
# If a process of the same name and PID as the extension
# of the trace file is executing, then keep that file
# otherwise remove it
#
#set -x

echo "Archiving Start Date/Time " `date`
echo "Archiving  redundant das log files files" `date +%Y-%m-%d`

cd /export/home/das/current-das/das/log

if [ ! -d history ]
then
    mkdir -p history/$(date '+%Y-%m')
    if [ $? -ne 0 ]
    then
        echo "\aUnable to create history directory. No history will be saved"
    else
        HISTORY=1
    fi
else
    HISTORY=1
fi

gzip *.log.2013*
echo "All files archived to gz"
echo "Moving files to history folder"
mv *.gz history/$(date '+%Y-%m')
echo "All Files Moved"
echo "Archiving End Date/Time" `date`
exit 0
What will be there in place of bold ones where HISTORY=1 is written in the script?
 
Old 08-13-2013, 04:36 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
I'm not sure what you want/need with the HISTORY=1 part.

Without details I can only give a general solution for the creation of the directories:
Code:
#!/bin/sh

cd /export/home/das/current-das/das/log

# does history dir exist:
if [ ! -d history ]
then
  # no history dir
  mkdir history
  if [ "$?" != "0" ]
  then
    echo "history Oops...."
    exit 1
  fi
  # if crontab is run as root user:
#  chown valid_user:valid_group history
#  chmod valid_perms history
fi

# does year-month directory exist:
if [ ! -d history/$(date '+%Y-%m') ]
then
  # no year-month dir
  mkdir history/$(date '+%Y-%m')
  if [ "$?" != "0" ]
  then
    echo "year-month Oops...."
    exit 1
  fi
  # if crontab is run as root user:
#  chown valid_user:valid_group history/$(date '+%Y-%m')
#  chmod valid_perms history/$(date '+%Y-%m')
fi

echo "archiving part goes here"

exit 0
About the red parts: If this script is run from root's crontab then the red parts are needed otherwise the created directories will be owned by root and thus not writeable by other users. Uncomment the chown and chmod lines and replace the valid_* parts with the appropriate values. I would strongly suggest running this from the users crontab (the user that owns this application. das?).

As you can see I did not use HISTORY=1 in any way. I do wonder if all this cannot be done more elegant/better, but without details I cannot point you in any direction.
 
1 members found this post helpful.
Old 08-13-2013, 09:28 AM   #8
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Hi druuna,

This is all what I need to ask from you. I just wanted to ask whether HISTORY=1 was correct there or not.
Thanks for you all help!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Hp Puts Solaris on More X64 Servers, Partners for Solaris Emulation LXer Syndicated Linux News 0 02-07-2007 07:21 AM
great solaris website for newbies http://solaris-x86.org/ feetyouwell Solaris / OpenSolaris 2 12-06-2006 12:14 AM
odd recursion: calling "by hand" vs calling by cronscript... prx Programming 4 02-12-2005 04:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration