Get a Unix script to run at exactly the same time every time - unix

I am writing a script to capture disk usage on a system (yes, I know there is software that can do this). For database reporting purposes, I want the interval between data points to be as equal as possible. For example, if I am polling disk usage every 10 minutes, I want every data point to be YYYY-MM-DD HH:[0-5]0:00. If I'm am polling every 5 minutes, it would be YYYY-MM-DD HH:[0-5][05]:00.
If I have a ksh script (or even a Perl script) to capture the disk usage, how can I let the script come active and wait for the next "Poll time" before taking a snapshot, and then sleep for the correct number of seconds until the next "Poll time". If I am polling every 5 minutes, and it is 11:42:00, then I want to sleep for 180 seconds so it will take a snapshot at 11:45:00 - and then sleep for 5 minutes so it will take another snapshot at 11:50:00.
I wrote a way that works if my poll time is every 10 minutes, but if I change the poll time to a different number, it doesn't work. I would like it to be flexible on the poll time.
I prefer to do this in shell script, but if it is way too much code, Perl would be fine too.
Any ideas on how to accomplish this?
Thanks in advance!
Brian
EDIT: Wow - I left out a pretty important part - that cron is disabled, so I will not be able to use cron for this task. I am very sorry to all the people who gave that as an answer, because yes, that is the perfect way to do what I wanted, if I could use cron.
I will be using our scheduler to kick off my script right before midnight every day, and I want the script to handle running at the exact "poll times", sleeping in between, and exiting at midnight.
Again, I'm very sorry for not clarifying on crontabs.

cron will do the job.
http://en.wikipedia.org/wiki/Cron
Just configure it to run your ksh script at the times you need and you are done

You might want to consider using cron. This is exactly what it was made for.

If I were doing this, I would use the system scheduler (cron or something else) to schedule my program to run every 180 seconds.
EDIT: I might have misunderstood your request. Are you looking more for something along the following lines? (I suspect there is a bug or two here):
ANOTHER EDIT: Remove dependency on Time::Local (but now I suspect more bugs ;-)
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw( strftime );
my $mins = 5;
while ( 1 ) {
my ($this_sec, $this_min) = (localtime)[0 .. 1];
my $next_min = $mins * ( 1 + int( $this_min / $mins ) );
my $to_sleep = 60 * int( $next_min - $this_min - 1 )
+ 60 - $this_sec;
warn strftime('%Y:%m:%d %H:%M:%S - ', localtime),
"Sleeping '$to_sleep' seconds\n";
sleep $to_sleep;
}
__END__

Have it sleep for a very short time, <=1 sec, and check each time whether poll time has arrived. Incremental processor use will be negligible.
Edit: cron is fine if you know what interval you will use and don't intend to change frequently. But if you change intervals often, consider a continuously running script w/ short sleep time.

Depending on how fine grained your time resolution needs to be, there may be a need to write your script daemon style. Start it once, while(1) and do the logic inside the program (you can check every second until it's time to run again).
Perl's Time::HiRes allows very fine granularity if you need it.

Related

Is there a way to measure or monitor a scheduler job's resources used?

Recently i got the task to optimize a quite huge PLSQL script which prior to my changes took about 1 hour +/- 10mins.
So I got to do some reallocation of some methods and generally just some replacement of big views with simpler subquery or with statements. I noticed that if I ran the scheduled job by right-clicking it and execute job I would in most cases see the run duration change (in a positive way). But if I enabled the job and let it run by its schedule it takes the original hour no matter what changes you do to it.
Now my question here is: Is there any way to monitor the RAM or CPU usage of the session/job or is there a difference in general how many resources are allocated to background processes? Because my suspicion here is the "manual" run job somehow gets some priorities the scheduler doesn't get or doesn't take.
Either way for troubleshooting purposes you can't take a few hours a work day just to wait for results.

Kill job if execution time less than 2 min

I have a chain with several jobs, and sometimes a certain job that normally takes about 2 hours, finishes in less than 2 minutes.
What I would like to do is to kill this job if it ends in less than 2 minutes so that the chain won't proceed.
Is this possible?
Thanks
Well you don't really want to kill anything, do you? If you do, see BMC's note (including video) on using ctmkilljob.
In this case your next job is dependent on 2 things, the predecessor job being ok and the duration of the predecessor job. Add another input condition to your next job (in addition to the existing condition) to represent the >2 minutes duration.
On the job that needs to run for more than 2 minutes, add a Notify when the Exectime exceeds 2 mins (or 60 mins or whatever you decide is the threshold) and get it to shout to an entry in your shout destination table.
On the Control-M Server create a new program entry in your shout destination table and create a small script referenced by the shout. The script should use the ctmcontb utility to create a new ODAT condition that your next is waiting on. If you have a look at the BMC help note for ctmkilljob (and just substitute in ctmcontb) then you'll see how to do this.

wp_schedule_event and Daylight Savings

So I ran into an issue on a website this morning that uses WP Cron. The code looks like this:
//Schedule the cron to run if not scheduled
if ( ! wp_next_scheduled( 'my_check_cron_event' ) ) {
wp_schedule_event( strtotime('tomorrow America/New_York') , 'daily', 'my_check_cron_event' );
}
The issue is pretty simple. That code works fine, essentially I want to run the function at midnight local time, every day. So when I first run the function - it gets the timestamp for midnight tomorrow - local time. And ever since - the functino has been running.
Now this weekend - because of daylight savings time, the function doesn't actually ever get RESCHEDULED - its just set to run every 24 hours. So the function started running at 11pm. Of course that's easy to fix - but will just break again. I can't really find any good posts on the topic - how would you deal with:
wp_schedule_event running daily when daylight savings time changes?
Just to preempt an obvious answers here: I can't run a proper cron - it needs to be done through wp. Our host simply doesn't support it, it sucks - but that's the client. Any advice would be greatly appreciated!
So for anyone coming across this, I had to come back to this for a permanent solution today - as Daylight savings is about to change! The root issue here is that wp_schedule_event - accepts a parameter thats a number of seconds until the next run. That parameter is saved to the cron array - and it really doesn't care what timezone you're in. It just keeps running at that interval. In order to fix this - I had to switch to wp_schedule_single_event.
It seems counter-intuitive, because we want this to run on an interval. But the difference is - we NEED to recalculate the next timestamp to run at:
if ( ! wp_next_scheduled( 'my_check_cron_event' ) ) {
wp_schedule_single_event( strtotime('tomorrow America/New_York'), 'my_check_cron_event' );
}
The difference here is strtotime() will get the correct timestamp each DAY - accounting for daylight savings, rather than simply running every 24 hours. Hope that helps someone!

How to Terminate PINTool after specified amount of time

My requirement is to run Intel PIN tool for specified amount of time lets say around 1 minute and then terminate.
For example:
I want to run notepad.exe for 1 minute under PIN. After 1 minute do post processing and close the log files properly and terminate notepad.exe using PINTool.
Use PIN_ExitApplication() to achieve this. You can perform post processing in the Fini callback.

How to run a command in python after a certain time?

I am currently working on a code where I want to implement something like this:
if current_time is more that 12_am:
hello = "world"
Basically I want to set or change a variable if the current time in my computer is more than 12 am.
I did some research on datetime, however I could not find a function that does as told above.
If there is already an answer to this, please do redirect me and excuse my clumsiness.
The script needs to be running consantly.
You can set the check to True and then maybe break the while loop. All depeneds on your application.
import time
while True:
if int(time.strftime("%H")) > 12:
check = True
Let's say you are writing a program for Raspberry and you control some lights and you turn them on only after a certain time. On microcontrollers the programs always run endlessly.
You need to constantly run your script and check whether date is more than 12 am.
Another aproach would be to check how much time left and sleep for that long.

Resources