Create new file when time change


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create new file when time change
# 1  
Old 04-23-2013
Create new file when time change

Hi,
I have one script, which is tailing from a log file. And I want to separate the output in different files based on the timestamp. Something like that:
Code:
./script.sh >> log_`date '+%Y%m%d%H'`.log

When I start the script it creates file, e.g. log_2013042314.log, and it is appending the output from the tail command. But once the hour is changed it stops. And in that case, when hour is changed, I would like to continue creating new file log_2013042315.log. Any ideas how can I do that?

Thanks!
# 2  
Old 04-23-2013
Here is a way to change the name of the log file:
Code:
log_file=log_`date '+%Y%m%d%H'`.log
old_hour=`date '+%H'`

while sleep 10; do
  new_hour=`date '+%H'`
  if [ $new_hour -ne $old_hour ]; then
    log_file=log_`date '+%Y%m%d%H'`.log
    old_hour=$new_hour
  fi
done

But it seems problematic stopping and starting the tail -f command. What if you just periodically create your separate hourly files in a batch process, possibly at the end of the day? Besides being easier to do, that would also seem a more reliable way to ensure none of the records end up in the wrong hourly file.
This User Gave Thanks to hanson44 For This Post:
# 3  
Old 04-23-2013
do you need to get log every 1 second , use this %Y%m%d%H%S

Code:
./script.sh >> log_`date '+%Y%m%d%H%S'`.log

# 4  
Old 04-24-2013
Thanks Hanson44! It works OK, I just change it to:
Code:
i=1
while [ i=1 ]

# 5  
Old 04-24-2013
I'm really glad it's working. Smilie And your addition makes it better. I do have one little suggestion. I would say:

[ 1 -eq 1 ] instead of [ i=1 ] and leave off the line where i is initialized.

Your syntax works, so in a way it's "right". But I have never seen [ i=1 ] syntax. It really surprised me and made me scratch my head, and I don't understand exactly why it works.

The other choice would be to say [ $i = 1 ] but then you might as well just say [ 1 -eq 1 ]
# 6  
Old 04-24-2013
Uppps, typo, my mistake....actually it is:
Code:
[ $i=1 ]

At the end I changed it to:
Code:
[ 1 -eq 1 ]

Thanks again!
# 7  
Old 04-24-2013
Quote:
Originally Posted by hanson44
Your syntax works, so in a way it's "right". But I have never seen [ i=1 ] syntax. It really surprised me and made me scratch my head, and I don't understand exactly why it works.
The test/[ command treats a single non-empty string argument as true, and a single empty string argument as false. i=1 is parsed as a single non-empty string.

For an infinite loop, I think it's best to use either while :; do ... or while true; do .... I believe it makes the intent clearer than testing something against itself.

apenkov:

With regard to the script, I hope this isn't something critical, because, as hanson44 points out, it is possible for entries to land in the wrong file. The batch processing suggestion is a much better idea.

Also, since it seems that the full script has not been posted, I can't say for certain what is going on, but, if the while-loop is spending most of its time checking for the start of a new hour, that's a job that is probably best handled by cron.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need a script to create file permission change restriction

Hello, I am looking for a UNIX shell script which can help me for access restriction. 1) /home/ram, there are number file with .txt extension, which should be only owned "ram" user. like as below ls -lrt *.txt -rwx------ 1 ram dba 11 Jan 4 2015 PASS1.txt -rwx------ 1 ram dba 10 Jan 4... (8 Replies)
Discussion started by: mr.trilok
8 Replies

2. Shell Programming and Scripting

How to change modification time of file?

Explain it with proper e.g (4 Replies)
Discussion started by: sidpatil
4 Replies

3. Shell Programming and Scripting

Create file base on server time

Hi Guys, Server time:- >date >Wed Nov 14 11:56:23 EST 2012 I want to convert in to Below Formet :- 20121114.1100 And cretae file base on above data : last 48 Hour 20121114.1000 20121114.1100 20121114.0900 20121114.1000 20121114.0800 20121114.0900 20121114.0700 20121114.0800... (2 Replies)
Discussion started by: asavaliya
2 Replies

4. Shell Programming and Scripting

How to create tgz file for all the directories in one time?

Hi, On server there is an one folder . which contains sub folder Eg - TEST folder contains test1, test2, execr ,tt (folder). also includes some text file like abc.txt psp.txt gg.log. here iwant to create tgz file for all the folders and file in one time. I know the command... (1 Reply)
Discussion started by: aish11
1 Replies

5. Shell Programming and Scripting

Change time stamp of a file

Hi, As i know , we can change the time stamp of a file by touch command, i did change in a file and it is looking as given # ls -l abcd -rw-r--r-- 1 batsoqa sicusers 0 Feb 17 2010 abcd actually i want to see the output like this -rw-r--r-- 1 batsoqa sicusers ... (3 Replies)
Discussion started by: apskaushik
3 Replies

6. Homework & Coursework Questions

Create file and then change the extension case.

Interpreter should be bash. 1. The problem statement, all variables and given/known data: I need to make a file (myText.txt or song.mp3 or cloud.tar.gz or whatever) and then change the extension to (myText.TXT , song.MP3, cloud.TAR.GZ). It would be good if I can add all information in... (4 Replies)
Discussion started by: Kdenmen
4 Replies

7. UNIX for Dummies Questions & Answers

How to change the file modification time of a file on nfs mount point

Hi I am accessing a file on nfs mounted device, after completing using of the file, i am tring to restore the access time and modification times of the file. So i got the previous modified time of the file using stat() function and trying to set the date and time for the file, To set these... (6 Replies)
Discussion started by: deepthi.s
6 Replies

8. Shell Programming and Scripting

change the time stamp of file

can we change the timestamp of a file to old date. -rwxrwxrwx 1 root other 330 Jul 1 16:03 abc.txt it shows creation time is 16.03 can i change it to previous time :) (2 Replies)
Discussion started by: anish19
2 Replies

9. Shell Programming and Scripting

How can I create a file with current time - 60 minutes

I'm using k-shell in unix and I want to create a file with the current system time - 60 minutes. I know I can use touch to create the file, but I'm having trouble specifying how tell it to use the current time less 60 minutes. Any ideas??? (4 Replies)
Discussion started by: DaveyTN
4 Replies

10. UNIX for Dummies Questions & Answers

time stamp of file create

Hi, Sounds a simple request but I also need (would like) to gather the seconds too. I'm not even sure if this is held. I would think it is, somewhere??!!?! I belive that stat would/could work but I don't do C (we'll not yet). Is there any comamnd line util I can use? SunOS. Cheers... (7 Replies)
Discussion started by: nhatch
7 Replies
Login or Register to Ask a Question