Shell script and crontab question - Create new output file everyday


Im looking for a way to have a command running 24hrs per day and generating an output file per day.

The command Im talking about is this one:

# ngrep -O dump.pcap -W byline -d xxxx port 99999
interface: xxxx
filter: (ip) and ( port 99999)
output: dump.pcap   <--- output file

So the goal would be to have it running all day long and every end of day it would generate a new file.. (not sure if its possible or not...)...
I thought on having something on the crontab that somehow would do this "switch" of files every end of day...

Just to illustrate what Im talking about if its not clear...:

1- The command would inside  script that would run on the background.. everyday on the crontab (not sure if its the best solution - the crontab one)

2- Every end of day it would generate a new output file (not sure if this is a code to go in the "script" itself or some command on the crontab).. so I would have:
dump<date_day_2>.pcap (created on the start of the "day 2"..

3- What would be your suggestion to have something that would delete files older than 10 days from the "script" output files? Something on the script itself? If so, can you suggestion a command line for this?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you don't mind losing a few packets at switchover time (between one ngrep stopping and the next starting), you could have this script:


# Stop any old ngrep commands running
pkill ngrep

# Start a new ngrep, in the background, with the output dated:
nohup ngrep -O  dump_$(date '+%Y%m%d').pcap -W byline -d xxxx port 99999 &

# Check for a maximum of 10 files, including the one just opened
if [ $(ls dump*.pcap  2>/dev/null | wc -l) -gt 10 ]; then
  rm $(ls -t dump*.pcap| tail +11)

Open in new window

Then run that script in crontab:
    0 0 * * * /path/to/above/scriptsh

(edited, because I forgot the "dump*.pcap" in the "rm" command - would have deleted other files in this directory!)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joaotellesAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.