Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Would like script to automatically restart every 24 hours

Status
Not open for further replies.

clacasse

Programmer
Aug 22, 2003
7
US
The following script (which I was able to put together thanks to this forum) saves the capture data to a file and names the file after the current date and time. I would like to have the script automatically stop and start each day (ie: 8:00am) so it will start a new capture file daily. Any ideas on the best way to do this? Thanks.

proc main
string DateString, TimeString, sCap
string CapName ; Name of capture file to open.

; Grab current time and date and place them into those string variables
ltimestrs $LTIME DateString TimeString

; I found out that Procomm does not like slashes or colons in capture filenames
; so lets replace all of these offending delimiters with hyphens
strreplace DateString "/" "-" ;Replace all slashes in date string with hyphens
strreplace TimeString ":" "-" ;Replace all colons in time string with hyphens

; Now concatenate the capture filename together
strcat CapName "Procomm_Capture_"
strcat CapName DateString
strcat CapName "_"
strcat CapName TimeString
strcat CapName ".cap"

set capture overwrite on ; overwrite old capture file data with new data
set capture query off ; do not prompt user for capture filename
set capture file CapName ; Set name of capture file (pieced together from above).
capture on ; Open up the capture file.

; We need to set up a "hook" to allow the capture file to be closed when a user
; stops the script.
when USEREXIT call close_capture

; Main capture loop. This is an infinite loop that grabs the current time, formats
; and dumps a time stamp string into the capture stream and then sleeps for 60 seconds
; before it does it again. This loop will run until the user stops the script or
; exits Procomm
while 1
ltimestrs $LTIME DateString TimeString
strfmt sCap "`r`nProcomm PC clock time: %s %s`n`n`r" DateString TimeString
termwrites sCap
pause 60
endwhile

endproc

proc close_capture
capture off ;close capture file
halt
endproc
 
If you add the following code to your script, just after the ltimestrs command in the while 1 loop, then the script will close the current capture file and exit the while loop. The way your script is written now will cause the script to exit. However, if you enclose the the main procedure inside a second while 1 loop, your script will pickup at the beginning each time that the new code exits the existing while loop.

ltimestrs $LTIME DateString TimeString
if strnicmp TimeString "8" 1
if strfind TimeString "AM"
capture OFF
exitwhile
endif
endif

aspect@aspectscripting.com
 
Thank you for your help. I made the changes and it compiled with no errors and is now running. I've pasted it below just in case you see something I did wrong. Thanks again.

proc main

string DateString, TimeString, sCap
string CapName ; Name of capture file to open.

while 1
; Grab current time and date and place them into those string variables
ltimestrs $LTIME DateString TimeString

; I found out that Procomm does not like slashes or colons in capture filenames
; so lets replace all of these offending delimiters with hyphens
strreplace DateString "/" "-" ;Replace all slashes in date string with hyphens
strreplace TimeString ":" "-" ;Replace all colons in time string with hyphens

; Now concatenate the capture filename together
strcat CapName "Procomm_Capture_"
strcat CapName DateString
strcat CapName "_"
strcat CapName TimeString
strcat CapName ".cap"

set capture overwrite on ; overwrite old capture file data with new data
set capture query off ; do not prompt user for capture filename
set capture file CapName ; Set name of capture file (pieced together from above).
capture on ; Open up the capture file.

; We need to set up a "hook" to allow the capture file to be closed when a user
; stops the script.
when USEREXIT call close_capture

; Main capture loop. This is an infinite loop that grabs the current time, formats
; and dumps a time stamp string into the capture stream and then sleeps for 60 seconds
; before it does it again. This loop will run until the user stops the script or
; exits Procomm
while 1

ltimestrs $LTIME DateString TimeString
if strnicmp TimeString "8" 1
if strfind TimeString "AM"
capture OFF
exitwhile
endif
endif

strfmt sCap "`r`nProcomm PC clock time: %s %s`n`n`r" DateString TimeString
termwrites sCap
pause 60
endwhile
endwhile

endproc

proc close_capture
capture off ;close capture file
halt
endproc
 
I think I may have done something wrong. The capture stopped and closed as it should this morning at 8:00AM, but when it started again it would run for a few seconds then stop and create a new file, and so and and so forth until it gave the following error:


"String length limit exceeded."

I've included the filenames it created below:

Procomm_Capture_11-29-04_4-29-18PM.cap (This is one I started manually yesterday. It stopped at 8:00am)

Procomm_Capture_11-29-04_4-29-18PM.capProcomm_Capture_11-30-04_8-00-16AM.cap

Procomm_Capture_11-29-04_4-29-18PM.capProcomm_Capture_11-30-04_8-00-16AM.capProcomm_Capture_11-30-04_8-00-16AM.cap

Procomm_Capture_11-29-04_4-29-18PM.capProcomm_Capture_11-30-04_8-00-16AM.capProcomm_Capture_11-30-04_8-00-16AM.capProcomm_Capture_11-30-04_8-00-16AM.cap

Procomm_Capture_11-29-04_4-29-18PM.capProcomm_Capture_11-30-04_8-00-16AM.capProcomm_Capture_11-30-04_8-00-16AM.capProcomm_Capture_11-30-04_8-00-16AM.capProcomm_Capture_11-30-04_8-00-16AM.cap

Procomm_Capture_11-29-04_4-29-18PM.capProcomm_Capture_11-30-04_8-00-16AM.capProcomm_Capture_11-30-04_8-00-16AM.capProcomm_Capture_11-30-04_8-00-16AM.capProcomm_Capture_11-30-04_8-00-16AM.capProcomm_Capture_11-30-04_8-00-16AM.cap
 
I haven't had chance to test this, but I think the modified script below should work. What I did was add a global integer to act as a flag to indicate if the capture file for that day had been opened already or not. I added a when $DATE command to the beginning of the script, which will cause the procedure clear_flag to be called when the date changes at midnight. This procedure will then clear this flag. When the code that checks for 8 AM runs, the first time it will run through as expected and set that flag value. The next time it runs (8:01 or so), the code will not be executed since the flag is set.

integer dateflag

proc main

string DateString, TimeString, sCap
string CapName ; Name of capture file to open.

when $DATE call clear_flag

while 1
; Grab current time and date and place them into those string variables
ltimestrs $LTIME DateString TimeString

; I found out that Procomm does not like slashes or colons in capture filenames
; so lets replace all of these offending delimiters with hyphens
strreplace DateString "/" "-" ;Replace all slashes in date string with hyphens
strreplace TimeString ":" "-" ;Replace all colons in time string with hyphens

; Now concatenate the capture filename together
strcat CapName "Procomm_Capture_"
strcat CapName DateString
strcat CapName "_"
strcat CapName TimeString
strcat CapName ".cap"

set capture overwrite on ; overwrite old capture file data with new data
set capture query off ; do not prompt user for capture filename
set capture file CapName ; Set name of capture file (pieced together from above).
capture on ; Open up the capture file.

; We need to set up a "hook" to allow the capture file to be closed when a user
; stops the script.
when USEREXIT call close_capture

; Main capture loop. This is an infinite loop that grabs the current time, formats
; and dumps a time stamp string into the capture stream and then sleeps for 60 seconds
; before it does it again. This loop will run until the user stops the script or
; exits Procomm
while 1

ltimestrs $LTIME DateString TimeString
if dateflag == 0
if strnicmp TimeString "8" 1
if strfind TimeString "AM"
capture OFF
dateflag = 1
exitwhile
endif
endif
endif

strfmt sCap "`r`nProcomm PC clock time: %s %s`n`n`r" DateString TimeString
termwrites sCap
pause 60
endwhile
endwhile

endproc

proc close_capture
capture off ;close capture file
halt
endproc

proc clear_flag
dateflag = 0
endproc

aspect@aspectscripting.com
 
Thanks for your help. I tested the code and it works wonderfully except it is not resetting the filename, just adding to it when it starts again at 8:00AM. Like this:

Procomm_Capture_12-1-04_1-40-41PM.capProcomm_Capture_12-2-04_8-00-38AM.cap

I tried to modify the code to clear the variables before it runs through the loop again but it still did the same thing. Perhaps my method of clearing the variables is incorrect. Here is what I came up with:

integer dateflag
string DateString, TimeString, sCap
string CapName ; Name of capture file to open.

proc main

when $DATE call clear_flag

while 1
DateString = ""
TimeString = ""

; Grab current time and date and place them into those string variables
ltimestrs $LTIME DateString TimeString

; I found out that Procomm does not like slashes or colons in capture filenames
; so lets replace all of these offending delimiters with hyphens
strreplace DateString "/" "-" ;Replace all slashes in date string with hyphens
strreplace TimeString ":" "-" ;Replace all colons in time string with hyphens

; Now concatenate the capture filename together
strcat CapName "Procomm_Capture_"
strcat CapName DateString
strcat CapName "_"
strcat CapName TimeString
strcat CapName ".cap"

set capture overwrite on ; overwrite old capture file data with new data
set capture query off ; do not prompt user for capture filename
set capture file CapName ; Set name of capture file (pieced together from above).
capture on ; Open up the capture file.

; We need to set up a "hook" to allow the capture file to be closed when a user
; stops the script.
when USEREXIT call close_capture

; Main capture loop. This is an infinite loop that grabs the current time, formats
; and dumps a time stamp string into the capture stream and then sleeps for 60 seconds
; before it does it again. This loop will run until the user stops the script or
; exits Procomm
while 1

ltimestrs $LTIME DateString TimeString
if dateflag == 0
if strnicmp TimeString "8" 1
if strfind TimeString "AM"
capture OFF
dateflag = 1
exitwhile
endif
endif
endif

strfmt sCap "`r`nProcomm PC clock time: %s %s`n`n`r" DateString TimeString
termwrites sCap
pause 60
endwhile
endwhile

endproc

proc close_capture
capture off ;close capture file
halt
endproc

proc clear_flag
dateflag = 0
endproc

As I was previewing this post I just realized I am not clearing the filename variable (CapName), but only clearing the variables that are used to build the name (DateString & TimeString). Perhaps thats my problem. I'll try it now.
 
Yes, if you change the line:

strcat CapName "Procomm_Capture_"

to read:

CapName = "Procomm_Capture_"

then I think this should clear up the capture name problem.


aspect@aspectscripting.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top