ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
Rep:
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.
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.
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
Original Poster
Rep:
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.
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.
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
Original Poster
Rep:
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.
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
Original Poster
Rep:
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.