Solved

shell script for checking

Posted on 2011-03-02
13
363 Views
Last Modified: 2012-05-11
I need a shell script for checking the  particular directory on the server and if there is a core dump file (like core.123 ), it should send an email to me.
I am not sure i will setup a cronjob to execute that shell script every one hour to see the if there are any core dump files, if you have any best practices please let me know.

This shell will run redhat linux 5 server

0
Comment
Question by:wasman
  • 6
  • 4
  • 3
13 Comments
 
LVL 9

Expert Comment

by:AriMc
ID: 35022690
Try this script, changing the directory name and your e-mail address to appropriate:


#!/bin/sh

if [ -f /tmp/core* ]
then
   echo "Core files exist" | mail -s "Core files exist" myemail@mydomain.com
fi

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 35022748
You can't use wildcards in an 'if' test.

#!/bin/bash

if [ -n "$(find /some/dir -type f -maxdepth 1 -name core\*)" ]
then
    echo | mail -s "One or more core files exist"  somebody@example.com
fi

Open in new window


0
 
LVL 9

Expert Comment

by:AriMc
ID: 35022771
Really? It works fine on my Debian.

0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 35022996
What Debian version and what shell?

I'm using Debian Squeeze.  Certainly won't work with bash, but will work with ksh, but I would be very surprised if you had overwritten the /bin/sh symlink with /bin/ksh on your system.

$ cat check
if [ -f core* ]
then
   echo matched
fi

Open in new window



$ ls -1
check
core
core.123
core.456
$ sh check
[: 4: core: unexpected operator
$ bash check
check: line 1: [: too many arguments
$ ksh check
matched

Open in new window

0
 
LVL 9

Expert Comment

by:AriMc
ID: 35023112
I'm using Debian Lenny kernel 2.6.26-1-686 and /bin/sh points to the original /bin/bash, see the output below.

Checking the existance of /tmp/core* files with ls:

$ ls -al /tmp/core*
-rw-r--r-- 1 root root 51 2011-03-03 01:40 /tmp/coretemp.txt

Then with a script for non-existing files:

$ cat check
#!/bin/sh

if [ -f /tmp/corez* ]
then
   echo "Yes"
fi

$ ./check
(No output)

Then with the a modified script for existing file:

$ cat check
#!/bin/sh

if [ -f /tmp/core* ]
then
   echo "Yes"
fi

~/tmp$ ./check
Yes








myhost:~/tmp$ uname -a
Linux myhost 2.6.26-1-686 #1 SMP Sat Jan 10 18:29:31 UTC 2009 i686 GNU/Linux

myhost:~/tmp$ ls -al /bin/sh
lrwxrwxrwx 1 root root 4 2009-03-08 20:34 /bin/sh -> bash

myhost:~/tmp$ ls -al /bin/bash
-rwxr-xr-x 1 root root 700492 2008-05-12 22:02 /bin/bash

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 35023789
Do a

touch /tmp/core1

and rerun.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:wasman
ID: 35024050
Folks thanks a lot for compassion in scripting world,
I forgot to mention one important thing, the script should to be smart enough to know the latest core dump that was produced at last in that directory. Bcz in that directory there will be always some old core dump files exists in that directory.


0
 
LVL 48

Expert Comment

by:Tintin
ID: 35025524
What do you want to do with the latest core file?
0
 

Author Comment

by:wasman
ID: 35027377
the server will be running fine, but sometimes it produces core dump once in a week,
that core dump will make disk out of space and the log don't get written, let's say the core dumps gets produced in /usr/local/web directory, there will be always some
old core dumps will be residing in /usr/local/web directory, for some reason development team want to delete them. So what i want is i will setup a cronjob, that will call
this shell script everyone hour, so the script will check the /usr/local/web directory for any latest core dump files, if they are then it will send email to me and go i delete them or move them.
0
 

Author Comment

by:wasman
ID: 35027399
updated answer

the server will be running fine, but sometimes it produces core dump once in a week,
that core dump will make disk out of space and the log don't get written, let's say the core dumps gets produced in /usr/local/web directory, there will be always some
old core dumps will be residing in /usr/local/web directory, for some reason development team don't want to delete them. So what i want is i will setup a cronjob, that will call
this shell script everyone hour, so the script will check the /usr/local/web directory for any latest core dump files, if there is then it will send email to me and go i delete them or move them.
0
 

Author Comment

by:wasman
ID: 35037292
tintin,

Any updates!!!
0
 

Author Comment

by:wasman
ID: 35057807
not sure why you didn't responded...but i am closing the ticket.. Thanks for helping me out
0
 

Author Closing Comment

by:wasman
ID: 35057815
Thanks for you help
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now