Need a shell script to find 0 byte files in 5 servers


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need a shell script to find 0 byte files in 5 servers
# 1  
Old 08-19-2014
HP Need a shell script to find 0 byte files in 5 servers

Hi.....
Thanks for this wonderful forum....

My request:

I have toatally 5 unix servers in which many applications are working

I need to set a trap to identify 0 byte files if any are created proactively.

For example:

Code:
find /apps/fresco/ -mtime 1 -size 0c –print >> zerobytefile.log

This command will find zero byte file for last 1 day (no need to check entire directory every day and this is for /apps/fresco folder which will cover all the interfaces and will apply for all my servers) then filter this log file for any data and if any data is present then raise a trap.

Last edited by rbatte1; 08-19-2014 at 12:45 PM.. Reason: Put the code in CODE tags, removed formatting colours/fonts etc.
# 2  
Old 08-19-2014
Quote:
Originally Posted by ChandruBala73
I have toatally 5 unix servers in which many applications are working

I need to set a trap to identify 0 byte files if any are created proactively.

For example:

Code:
find /apps/fresco/ -mtime 1 -size 0c -print >> zerobytefile.log

this command will find zero byte file for last 1 day (no need to check entire directory every day and this is for /apps/fresco folder which will cover all the interfaces and will apply for all my servers)

2) then filter this log file for any data and if any data is present then raise a trap.
First off: welcome to the forum and i hope you enjoy the ride. Please do us (and yourself) a favour and use as little text markup as possible. For any code, terminal output or similar data use "CODE"-tags, which let stand out yor text nicely. I have taken the liberty and replaced this in the quoted text above. Compare yourself. Thanks for your consideration.

To your problem: you alread constructed a command to identify such files. Instead of the ">>" operator you used you should use ">" to create this file anew every day. Establish passwordless login through ssh for some user (likely root, but any other user with sufficient rights will do) at each of the target machines. Put this command into a loop on any system, like this (only a sketch):

Code:
# cat list_of_hosts_to_monitor
hostA
hostB
hostC
hostD

# cat mymonitoring.sh
#! /bin/ksh

typeset fHosts="/path/to/list_of_hosts_to_monitor"
typeset fOut="/path/to/output/dir"
typeset chUser="root"          # or something else
typeset chHost=""

while read chHost ; do
     ssh -o 'BatchMode = yes' "${chUser}@${chHost}" "find /apps/fresco/ -mtime 1 -size 0c -print" > "${fOut}/${chHost}.log"
done < "${fHosts}"

exit 0

This gives you a file for each host (named like the hostname+".log") locally (in the directory $fOut points to) which you can examine separately.

Then set up a cron job to run this script daily.

I hope this helps.

bakunin
# 3  
Old 08-19-2014
Thank you so much. I'm new to this forum I haven't expected such a quick response. I will learn quickly to post messages in better way.

'Establish passwordless' is not possible for me hence I have changed my mind to write a script for all 5 servers separately.

If I use that command mentioned earlier, some zero byte files are coming which is actually not in my scope. I want took all the paths which is not in our scope and now I want a script which should connect a single IP through ssh and gives me an output of zero byte file excluding the paths not in our scope which has zero byte.

Last edited by rbatte1; 08-19-2014 at 01:28 PM.. Reason: Capitalised first person singular and emboldened command names in text.
# 4  
Old 08-19-2014
Quote:
Originally Posted by ChandruBala73
'Establish passwordless' is not possible for me hence i have changed my mind to write a script for all 5 servers separately.
I am not sure why this is a bettwer way to do this. "passwordless" means: create a secure-key with ssh-keygen on the system you want to run the script from. The resulting file is usually located in /your/homedir/.ssh in a file named (if you use the default RSA encryption) id_rsa.pub. Open/create the fle /home/of/user/.ssh/authorized_keys on the remote machine you want to connect to and append the content of the local file id_rsa.pub there. Now the user from the system you generated the key at is allowed to connect as the user where you stored the keyfile at without password. The user is not identified by the password but by the key you transmitted.

Quote:
Originally Posted by ChandruBala73
If i use that command mentioned earlier , some zero byte files are coming which is actually note in my scope. I want took all the paths which is not in our scope and now i want a script which should connect a single IP through ssh and gives me an output of zero byte file excluding the paths not in our scope which has zero byte.
Actually the script i gave you does exactly this: it connects to a single machine, issues the command, saves its outcome, then goes on to do the same to the next machine on the list. To cut some paths from finds scope there are two ways: use the "-prune" option to prune away some paths you are not interested in or use grep -v to cut out from the output what doesn't peak your interest.

You might want to search this forum for "find" and "prune" with the "advanced search" feature to find out how this is done. It is the preferable way to do it.

I hope this helps.

bakunin

Moderator's Comments:
Mod Comment addendum: i just noticed that this has landed in the wrong forum. I move this over to "Unix for Dummies"
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find and removing the old files and zipping the files using shell script

Hi, I am trying to removing the old files which were older than 10 days and same g zipping the files using the shell script. script was return as follows. find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f | xargs rm -f find /cer_skyliv??/log... (6 Replies)
Discussion started by: venkat918
6 Replies

2. Shell Programming and Scripting

Shell script for connecting multiple servers and then copying 30 days old files

Shell script for connecting multiple servers and then copying 30 days old files from those server . HI , I have 6 multiple servers pla1,pla2,pla3,pla4,pla5,pla6 1. These six servers have common shared mount point /var/share 2. Running script from /var/share to connect these servers.I... (1 Reply)
Discussion started by: rcroyal88
1 Replies

3. Shell Programming and Scripting

Shell Script to delete files from 5 different servers

Hello, I'm new to shell scripting and need a quick note on how to write a shell script to perform deletion of files from 5 different hostnames in various locations. Found out to delete files from one path by using below command and made it to work on cron job but need to do it in a shell... (2 Replies)
Discussion started by: Teja G
2 Replies

4. Shell Programming and Scripting

Shell script to find filesystem capacity on 50 servers

Hi all, I am new to Unix and I want to write a shell script in a jumpbox for finding the filesystem capacity on 50 unix servers ( by ssh ) and then email the result in HTML format with server name and capacity % to a specific outlook distribution list. any suggestion would be of great help. (17 Replies)
Discussion started by: amitbisht9
17 Replies

5. Shell Programming and Scripting

shell script to take input from a text file and perform check on each servers and copy files

HI all, I want to script where all the server names will be in a text file like server1 server2 server3 . and the script should take servernames from a text file and perform copy of files if the files are not present on those servers.after which it should take next servername till the end of... (0 Replies)
Discussion started by: joseph.dmello
0 Replies

6. Shell Programming and Scripting

Shell Script - find, recursively, all files that are duplicated

Hi. I have a problem that i can't seem to resolve. I need to create a script that list all the files, that are found recursively, with the same name. For example if a file exists in more than one directory with the same name it list all the files that he founds with all the info. Could someone... (5 Replies)
Discussion started by: KitFisto
5 Replies

7. Shell Programming and Scripting

Shell script to find files

Hi Experts, I am trying to write a shell script that should 1. Find files with name (ab030.txt,Ab030.TXT,AB030.TXT,ab030.TXT,AB030.txt) 2. If any of the above found, rename it to AB030.TXT Thanks. (4 Replies)
Discussion started by: welldone
4 Replies

8. Shell Programming and Scripting

Check if 2 files are identical byte-to-byte?

In my server migration requirement, I need to compare if one file on old server is exactly the same as the corresponding file on the new server. For diff and comm, the inputs need to be sorted. But I do not want to disturb the content of the file and need to find byte-to-byte match. Please... (4 Replies)
Discussion started by: krishmaths
4 Replies

9. Filesystems, Disks and Memory

shell script to find zero byte files

I have a directory MYDIR In which i have several directories 1,2,3,4.... Now, In each of these directories i have several files a.dat, b.dat, c.dat, d.dat..... MYDIR ----1 ---------a.dat ---------b.dat ---------c.dat ---------d.dat ----2 ---------a.dat ---------b.dat ---------c.dat... (2 Replies)
Discussion started by: ramky79
2 Replies

10. Shell Programming and Scripting

shell script to find files

I have a directory which contains files with different kinds of extensions .everyday a file with .log gets added to it .i want to extract the file with .log extension which is created with todays date . thanks in advance (2 Replies)
Discussion started by: naren_samba2005
2 Replies
Login or Register to Ask a Question