LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-29-2014, 03:08 PM   #1
Learin
LQ Newbie
 
Registered: Apr 2014
Posts: 8

Rep: Reputation: Disabled
Shell scripting and log file redirecting and creation.


Hello everyone! I'm not exactly a newbie in linux but beyond basic navigation and day to day stuff I don't know much more. I am a technical analyst and I help maintain servers everyday. I have only been doing the job for a few months and I came across a problem that we are having that could be fixed by creating a shell script that will restart a service once a log file tied to that service reaches a certain size. I was wondering if anyone could help me figure out how to create a shell script that would monitor the log file and then restart the service that sends information to that log file once the log file reaches, lets say 2gb.

P.S.
Another question I have regarding log files is how to combine multiple log files into a new log file that I have created. Or even how to create a log file at all.

Thanks in advance for all of your input!
 
Old 04-29-2014, 03:16 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
My 2 cents on scripting are here in a blog entry.

My most poignant points there are the first two in bold:

Quote:
"All programming is the use of simple operations to solve complex problems through iteration or combination of those simple steps."
Quote:
"Whatever you put in a bash script is the same stuff you can type on a command line."


So if you can detect things in the log file, stop and restart the service via shell commands, you can script it.

In that blog are some example scripts, mainly used to make my self serving points, but intended to be instructive. They also should show basic scripts though.

Other references would be shell scripting web-references:
The Definitive Guide
Basic Shell Scripting Guide
Another Basic Shell Scripting Guide
 
1 members found this post helpful.
Old 04-29-2014, 03:19 PM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
Quote:
Originally Posted by rtmistler View Post
"Whatever you put in a bash script is the same stuff you can type on a command line."
Not really. There are plenty of script-centric commands that do not work on the command line.

I would edit this to reflect the possibility.
 
Old 04-29-2014, 03:30 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Quote:
Originally Posted by szboardstretcher View Post
Not really. There are plenty of script-centric commands that do not work on the command line.

I would edit this to reflect the possibility.
Thanks. I'd be happy to update that statement. Can you cite an example to help grow my knowledge there? I'll do some searching too, to try and make it a more correct statement. Well ... halfway there with that thinking, but is it not true that whatever you can type on a command line, you can script? What you're saying is that there are things you can do within a script which can't be done on a command line, at least not exactly in the same manner?

Sorry OP, hoping not to alter your original question/thread.
 
Old 04-29-2014, 03:30 PM   #5
Learin
LQ Newbie
 
Registered: Apr 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you for the information. I suspected that the scripts would contain the same basic syntax as the command line because a coworker put together a command that will output the size of the log after the specified amount of time. So i'm thinking I can use part of what he already has and then just add in the service restart part. I'll check out that link and go from there thank you very much.
 
Old 04-29-2014, 03:39 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
Quote:
Originally Posted by rtmistler View Post
Can you cite an example to help grow my knowledge there?
Certainly. 'getopts' would be one. Any of the parameter related commands and variables would not work from command line...

I would say that 'mostly' a script is just a collection of command-line's.. but a script also has some further architecture to it that makes it a bit of a programming language.

For a beginner though, thinking of it as a collection of commands is not far off. But a script can certainly grow into something more.

Last edited by szboardstretcher; 04-29-2014 at 03:44 PM.
 
1 members found this post helpful.
Old 04-29-2014, 04:51 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I am curious about the reasoning behind the question. My assumption (please correct if wrong) is that once the file reaches a certain size you pause / stop the service, move the log file (back it up or whatever is needed) and then restart the service to create / start a new logfile.

If my assumption is correct, could you not look at something like logrotate to successfully move / backup the current logfile whilst the service keeps running into a new file?
I am not 100% sure on how this works with capturing any data sent during the rotation process but sure someone here could lead you in the right direction.

Anyhoo, let us know a little more detail behind the reasoning being used so we may better assist?
 
1 members found this post helpful.
Old 04-29-2014, 05:14 PM   #8
Learin
LQ Newbie
 
Registered: Apr 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Well the way this particular log file works is, there is a service that runs constantly and it helps govern hundreds of processes in the server. The process is installed on thousands of servers. On each individual server the service has a log file that is written to whenever anything is done in one of the programs that it is linked to. Th main thing that causes it to fill up quickly is error logs. I cannot redirect any of the logs to a different file because the this is the way my company has it set up. What I commonly have to do when the log file fills up is restart the service which clears out the log file. When that log file gets full it slows the server to a crawl and because of this the rest of the computers on the network cease to function. What I am trying to figure out is how to create a script that will monitor this log and whenever the log reaches a certain size restart the service to clear the log. On the last software rollout my company increased the size limit of the log thinking it would fix the issue. But all it did was increase the amount of time it takes to fill up. Since I don't know much about scripting I wasn't sure exactly how to write this script. The script will only be a temporary fix until a new software update is rolled out but that will be some time from now.
 
Old 04-29-2014, 05:21 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Grail is right, you should consider logrotate for this. Can you just say erase the existing log file and that won't cause the service to have problems? If so, then that's pretty much what logrotate does, it moves or erases the file allowing the service to continue.

So what you can do is specify a profile file for logrotate to do this daily, weekly, hourly, however fast you wish and then rotate based on size or force rotation all the time, I think using size as a qualifier is fine and then you just need to determine how frequently you need to check for rotation. Further, you can keep a number of backups, as many as you want or have space for and specify that the backups be zipped. And you can use the rotation event to also call a script if you wish to then go further and mail, move, archive off-system; your backups.

If you have questions about logrotate, I can show some examples of all except the part where you'd trigger a script, I've specified multiple backups, frequencies of rotation, and whether or not to zip the backups, also where to store the backups.
 
1 members found this post helpful.
Old 04-29-2014, 05:27 PM   #10
Learin
LQ Newbie
 
Registered: Apr 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you both for the info. I'll give logrotate a try. It sounds like that will be the best solution to the problem. Examples of how logrotate works would be great. I've never used it before.

Thanks again for all of your help.
 
Old 04-29-2014, 05:39 PM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Here's an example rotate.cfg file, I forget all the specific details for instance if that's the default name, but you can specify the configuration file when you launch the daemon. I added the # comments, those aren't in my rotate.cfg file, just wrote them here for your benefit. Comments only allowed at the beginning of a line

Code:
daily                        # rotate daily
rotate 100                   # Number of backup files
olddir /<my-home/logs/old    # directory to store the backups in
missingok                    # indicates if no files are found not to barf and exit

/<my-home>/logs/*.log {      # indicates the specific files to rotate, location, search spec, one could also say one specific file name
    size 1M                  # indicates the maximum size to qualify for rotation
}

# a valid comment in your logrotate configuration file, the other comments would be illegal in-line as they are shown
See the manpage for logrotate as well, there are some examples there.

compress is the keyword to compress your backups.

And sorry, looks like daily is the fastest this will run, if you use the daemon. I run it on an embedded system where same condition, streaming logs can get large and that can't be allowed, so I manually run logrotate on that system once per minute, hence the reason for 100 backups of 1M, and in this case the daily argument has no effect. Therefore you can use a script and the argument for that script would be your config filename.
 
Old 04-29-2014, 05:53 PM   #12
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
logrotate alone probably won't solve the problem. All logrotate does is rename the file, if the service still has it open then this accomplishes nothing.

He would still need to write a script that logrotate would call which restarts the service, in which case the logrotate part of the operation is pointless and all he needs to do is write a short script that checks the log file size and restarts the service, which he can stick in a cron to run however often he wishes, which is what he was originally asking for.
 
Old 04-29-2014, 06:28 PM   #13
Learin
LQ Newbie
 
Registered: Apr 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Here's an example rotate.cfg file, I forget all the specific details for instance if that's the default name, but you can specify the configuration file when you launch the daemon. I added the # comments, those aren't in my rotate.cfg file, just wrote them here for your benefit. Comments only allowed at the beginning of a line

Thanks for the example that helps a ton. I think I'll be able to find a ton of uses for this.



Quote:
logrotate alone probably won't solve the problem. All logrotate does is rename the file, if the service still has it open then this accomplishes nothing.

He would still need to write a script that logrotate would call which restarts the service, in which case the logrotate part of the operation is pointless and all he needs to do is write a short script that checks the log file size and restarts the service, which he can stick in a cron to run however often he wishes, which is what he was originally asking for.

Yes the script that monitors the log and then restarts the service is the way I think I will have to go for this particular issue. Because of the way things are set up, and the fact that there are so many separate servers it would be more feasible to create a shell script to run whenever I happen to be logged into each server. But the I will be able to use the logrotate on my home servers for many various things.


Thank you both for the replies.
 
Old 05-23-2014, 03:51 PM   #14
PaBlO_r00t
LQ Newbie
 
Registered: Apr 2013
Posts: 4

Rep: Reputation: Disabled
#! /bin/bash
clear

function rotando() {
for ((contador=$1; contador>=1, contador--))
do
mv -f /$nombrefichero_$contador.gz $nombrefichero_`expr $contador + 1`.gz
done
mv -f $nombrefichero $nombrefichero_1 && gzip -f $nombrefichero_1
return 0
}

read -p "Nombre del servicio a controlar:" service
read -p "Nombre fichero log a controlar (ej: cron.log):_" fich
direct=/var/log
nombrefichero=$direct/$fich
limitSize=500000

size=`stat $nombrefichero | grep "Size" | cut -d ":" -f 2 | cut -d " " -f 2`
if [ $size -gt $limitSize ]
then
#...paramos servicio...
initctl stop $service >/dev/null 2>&1 || /etc/init.d/$service stop >/dev/null 2>&1

#...ultima version comprimida y rotada, maximo 4 rotaciones
versiones=`ls $nombrefichero_*.gz 2>/dev/null | wc -l`
if [ $versiones -eq 4 ]
then
rm $nombrefichero_4.gz
rotando `expr $versiones - 1`

else
rotando $versiones
fi

#....una vez rotados, nos creamos el fichero original vacio...
touch $nombrefichero

#...arrancamos servicio...
initctl start $service >/dev/null 2>&1 || /etc/init.d/$service start >/dev/null 2>&1

fi

Last edited by PaBlO_r00t; 05-23-2014 at 04:10 PM. Reason: error anterior post
 
Old 05-24-2014, 09:40 AM   #15
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,601

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Something you might look into depends on how the service works.

If it were written to use syslog for logging (much nicer than a custom log), you can have syslog forward the log data to a remote server.

This has four advantages, and one disadvantage.

advantages:
1) centralized log area
2) cycling the the log(s) on the log server is easier than trying to do that on all the servers.
3) gets all the logs as a consistent time/size based snapshot
4) preserves disk space on the members of the cluster

The disadvantage:
1) a fair amount of network traffic

It might be counted as a disadvantage of the existence of a log server, but usually there is already a management server in the system, it just might need more disk space.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Read .log format file and get special character from some lines by shell scripting samasara Linux - Newbie 32 12-21-2013 03:17 AM
wanna generate log file of inode creation under a directory qamarhafeez Linux - Server 1 01-20-2012 04:11 PM
Shell Scripting text file creation issue AiresTheBold Linux - Newbie 1 02-16-2011 10:27 AM
Redirecting log messages to other file devkpict Linux - Kernel 1 07-09-2008 03:38 PM
File Creation: bash shell stapletone Linux - Newbie 2 09-24-2006 01:35 PM


All times are GMT -5. The time now is 08:22 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration