Check if file is locked otherwise gzip it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if file is locked otherwise gzip it
# 1  
Old 03-16-2013
Check if file is locked otherwise gzip it

Dear community,
I've a continuos tcpdump on redhat that close the dumped file every 100000 captured packets.

To avoid disk full I would like to gzip the closed *.cap file. But how can I check if the file is currently opened by tcpdump and skip it from gzip?

Thanks!

EDIT: Just to post an example:
I have currently the following file on the trace directory:

Code:
-rwxr-xr-x 1 root root      249 Aug  9  2011 cont_tcpdump.sh
drwx------ 2 root root     4096 Dec 13  2011 lost+found
-rw-r--r-- 1 root root 16558574 Mar 16 18:37 tcpdump_continuous_20130316_183340.cap
-rw-r--r-- 1 root root 17322400 Mar 16 18:40 tcpdump_continuous_20130316_183708.cap
-rw-r--r-- 1 root root 15552512 Mar 16 18:44 tcpdump_continuous_20130316_184049.cap
-rw-r--r-- 1 root root  6029312 Mar 16 18:45 tcpdump_continuous_20130316_184437.cap

The last file (tcpdump_continuous_20130316_184437.cap) is currently opened by tcpdunp, so my other script should run every 5 minutes and gzip only the closed *.cap files!

Last edited by Lord Spectre; 03-16-2013 at 03:50 PM..
# 2  
Old 03-16-2013
Try:
Code:
fuser -- file


Last edited by Scrutinizer; 03-16-2013 at 04:07 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-16-2013
If you don't have fuser then lsof is another option:
Code:
for cap_file in tcpdump*.cap
do
        if lsof -f -- "$cap_file" > /dev/null
        then
                printf "File: $cap_file currently in use\n"
        else
                printf "File: $cap_file currently not in use\n"
                gzip "$cap_file"
        fi
done

This User Gave Thanks to Yoda For This Post:
# 4  
Old 03-16-2013
Thanks Scrutinizer,
fuser works perfect!
Btw, I realized that it output the file in use, now I've to exclude it from gzip!
Unfortunately gzip cannot exclude file.... Maybe fuser can show the file NOT in use?

---------- Post updated at 01:01 PM ---------- Previous update was at 12:56 PM ----------

OPS! Yoda, your solution works perfect thanks! Smilie

---------- Post updated at 01:37 PM ---------- Previous update was at 01:01 PM ----------

WOPS! Unfortunately lsof seems doesn't work from crontab... Smilie

If I execute the Yoda's script from shell it works, but at crontab level it just gzip all the *.cap files!!!

But why ?!?!? Smilie
# 5  
Old 03-16-2013
Quote:
Originally Posted by Lord Spectre
WOPS! Unfortunately lsof seems doesn't work from crontab... Smilie

If I execute the Yoda's script from shell it works, but at crontab level it just gzip all the *.cap files!!!

But why ?!?!? Smilie
Are you sure that you are using absolute path of capture files in your script?

You can also set DEBUGs and redirect the script output to another file in crontab to check what exactly is going on...
# 6  
Old 03-16-2013
Quote:
Originally Posted by Yoda
Are you sure that you are using absolute path of capture files in your script?
I think so, because the gzip works perfect except for the fact that gzip all the *.cap files even those that are in use.
This is my very simple script:

Code:
# pwd
/home/user/trace

# cat gzippa.sh 
#!/bin/bash
find /home/user/trace/*.cap.gz -type f -mmin +600 -exec rm -f {} \;
for cap_file in /home/user/trace/tcpdump*.cap
do
        if lsof -f -- "$cap_file" > /dev/null
        then
                printf "File: $cap_file currently in use"  > /dev/null
        else
                gzip -9 "$cap_file"
        fi
done

# 7  
Old 03-16-2013
Your script looks good & works for me!

I tested it from crontab on few files out of them I kept one file open and it skipped that one from gzipping!

Like I mentioned before, put some print debugs on your script and redirect o/p to another to understand what is going on:
Code:
* * * * * /home/user/trace/gzippa.sh > /home/user/trace/debug.out

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to check the file locked or in use at FTP?

Hi Experts, I am beginner to shell scripting, I have a archive script which will connect to the FTP server and archive the files from FTP source location to FTP archive location. Now the problem here is, the script working for few files and not working for few.I am facing the below error... (1 Reply)
Discussion started by: spidy
1 Replies

2. Shell Programming and Scripting

How to kill the process when the file is locked?

I was trying to read the file to create a table in SAS and I got error as follows while I read. Resource is write-locked by another user. File =/usr/sas/sas_config/Lev1/SASApp/StoredProcessServ​er/Logs/SASApp_STPServer_2015-09-29_tmp_18208.log. System Error Code = 0. ERROR: File is in... (10 Replies)
Discussion started by: Ram Kumar_BE
10 Replies

3. UNIX for Advanced & Expert Users

gzip vs pipe gzip: produce different file size

Hi All, I have a random test file: test.txt, size: 146 $ ll test.txt $ 146 test.txt Take 1: $ cat test.txt | gzip > test.txt.gz $ ll test.txt.gz $ 124 test.txt.gz Take 2: $ gzip test.txt $ ll test.txt.gz $ 133 test.txt.gz As you can see, gzipping a file and piping into gzip... (1 Reply)
Discussion started by: hanfresco
1 Replies

4. Solaris

gzip a file and append creation date stamp to file

I want to gzip a file and append the creation date to the end of the file. How can I accomplish this task. Basically they are log files which need a creation date stamp appended to make sure they do not overwrite other log files. -jack (3 Replies)
Discussion started by: jacktravine
3 Replies

5. UNIX for Dummies Questions & Answers

find locked files, print file path, unlock file

using OS X and the Terminal, I'd like to find all locked files in a specified directory, unlock them, and print a list of those files that were unlocked how can I do this? I'm familiar with chflags nouchg for unlocking one file but not familiar with unix enough to do what I'd like. Thanks! (0 Replies)
Discussion started by: alternapop
0 Replies

6. UNIX for Advanced & Expert Users

How to list locked file in unix?

Hi, I want to list out all files which are lock by some process. ex- ~/critical I want to list all files in critical directory which has been locked. I need this very badly. Any suggestion highly appriciated. Regards, Ashok (1 Reply)
Discussion started by: ashokd001
1 Replies

7. Shell Programming and Scripting

checking whether a file is locked

hi Guys, I just wondering how I can check and ensure a file is not locked by another process. I need to modify a file using sed but I need to ensure that is not being modified by another process at the same time. Thanks. Harby. (2 Replies)
Discussion started by: hariza
2 Replies

8. Shell Programming and Scripting

Lock a file. AND Wait if file is locked

Hi, I want to do the foll steps: 1. Check if someone has a lock on my file1. 2. if file1 is locked by any other user wait in a loop till another user releases lock 3. when lock released, lock file1. 4. do procesing (write) on file1. 5. processing complete. release lock on file1. ... (2 Replies)
Discussion started by: sunil_neha
2 Replies

9. UNIX for Dummies Questions & Answers

How can a file be locked for a certain time?

I want to be able to lock a file for 60 minutes so that an automated monitoring program will not execute the script more that once an hour. I have never used a lock file but have heard that is what I need to use. Does anyone have any examples of how I would use this? lock 60 filename.ksh ---?... (6 Replies)
Discussion started by: darthur
6 Replies

10. UNIX for Dummies Questions & Answers

How do I send a file as an attachment (gzip file) on a Unix system

Hi, How do I send a file as an attachment (gzip file) on a Unix system ? Using sendmail. Please help me. :confused: (3 Replies)
Discussion started by: lacca
3 Replies
Login or Register to Ask a Question

Featured Tech Videos