1838219 Members
3345 Online
110125 Solutions
New Discussion

time operations

 
SOLVED
Go to solution
Miquel_2
Advisor

time operations

Hi everybody,

Are there any expresion to add x seconds to current time ?

Thanks in advance.
16 REPLIES 16
john korterman
Honored Contributor

Re: time operations

Hi,
do you mean adding seconds to the system time? If yes, on 11.00 and onwards you can make the system clock tick faster until it has drifted a specified number of seconds ahead of it' prevoius time, e.g.
# date -a 45
which will slowly adjust the clock by addding 45 seconds.
Beware, that you get a prompt back immediately and you get no indication of when the "drift" has finished.

regards,
John K.
it would be nice if you always got a second chance
Miquel_2
Advisor

Re: time operations

I don't want to move system time, I only need to operate with it for queue jobs at specified time.
Simon Hargrave
Honored Contributor

Re: time operations

If you want a time-of-day for calculation purposes, then you can calculate the time of day in seconds with:

(( now = `date +%H` * 3600 + `date +%M` * 60 + `date +%S` ))

This will set $now to be the time of day in seconds. Add 45 to this to get the time in 45 seconds.

Then if you want to compare "45 seconds time" with eg 18:00:00 (or any arbitrary time) you can calculate the time to compare with:

(( comptime = 18*3600 + 0*60 + 0 ))

Obviously if comparing against fixed times you don't need the formula above in your script just calculate it, but the above formula lets you use hours/minutes/seconds.
john korterman
Honored Contributor

Re: time operations

if you want to change the file modification time, take a look at the touch command.

regards,
John K.

it would be nice if you always got a second chance
Keith Bryson
Honored Contributor
Solution

Re: time operations

Hi Miquel

Try this script:

#!/bin/ksh
H=`date +%H`
M=`date +%M`
SEC=`date +%S`
echo "Time is ${H}:${M}:${SEC}"
let HOUR=${H}*3600
let MIN=${M}*60
let TIME_IN_SECS=${HOUR}+${MIN}+${SEC}
let TIME_IN_SECS=${TIME_IN_SECS}+$1
let NEWHOUR=${TIME_IN_SECS}/3600
ADJ_HOUR=$NEWHOUR
LOOP=TRUE
while [ $LOOP = "TRUE" ]
do
if [ $ADJ_HOUR -gt 23 ]
then
let ADJ_HOUR=$ADJ_HOUR-24
else
LOOP=FALSE
fi
done

let TMPHOUR=$NEWHOUR*3600
let TMPMIN=${TIME_IN_SECS}-$TMPHOUR
let NEWMIN=${TMPMIN}/60
let TMPSEC=${NEWMIN}*60
let NEWSEC=${TMPMIN}-$TMPSEC
typeset -Z2 HOUR=$ADJ_HOUR
typeset -Z2 MIN=$NEWMIN
typeset -Z2 SEC=$NEWSEC
echo "Wound time is ${HOUR}:${MIN}:${SEC}"


Save it to a file, give execute permissions and call it with an argument (e.g. /tmp/myscript 500)

You will need to test though - if the time is wound to a subsequent day, the script won't tell you that, although it does report the time correctly!!

HTH - Keith
Arse-cover at all costs
Keith Bryson
Honored Contributor

Re: time operations

and yes, it is dirty (ksh) but I'm no perl expert...


All the best - KB
Arse-cover at all costs
A. Clay Stephenson
Acclaimed Contributor

Re: time operations

Note that if you are using this to queue jobs then you don't need to do anything. The "at" command understands expressions like "now + 90 seconds" or "now + 5 minutes" or "now + 2 hours". Man at for details.
If it ain't broke, I can fix that.
Miquel_2
Advisor

Re: time operations

Yes A.Clay Stephenson, this is what I want, but if try to queue jobs with "at now + n seconds" this error is reported:

$ at -qd now + 3 seconds < JOBS/job1.job
bad date specification

Looking at man at, the time specification seconds is not allowed, only minutes, hours, days.....

A. Clay Stephenson
Acclaimed Contributor

Re: time operations

You are correct. The standard at command's smallest unit is the minute. It really never occurred to me that you nould want to schedule a job only 3 seconds into the future. If that's the case then I would add something like:
sleep 3
at -f /usr/local/bin/myjob.sh now
to my script.

The "now" by itself is legal syntax for at.
If it ain't broke, I can fix that.
Miquel_2
Advisor

Re: time operations

Thanks, but I don't want to queue a job 3 seconds after now, I want all my queued jobs run in correct order and at the end of every queued job I need to add a script for reschedule the rest of jobs in queue with a difference of 3 seconds beacause the wait time for queue is 2 seconds and then I am sure jobs run in correct order.
Geoff Wild
Honored Contributor

Re: time operations

What you need is something like "Control M"

http://www.bmc.com/products/proddocview/0,2832,19052_19429_23437_1521,00.html

or, have 1 "Master" job - that calls all the other jobs - which waits for each job to finish prior to starting the next one...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
A. Clay Stephenson
Acclaimed Contributor

Re: time operations

Okay here's a quick Perl script which should do what you want and it formats the time specification in the format that at likes:
CCYYMMDDhhmm.ss

Use it like this:
TM=$(inctime.pl 5)
echo "Time 5 seconds from now = ${TM}"

------------------------------------------
#!/usr/bin/perl -w

use strict;
use integer;
use English;

sub Usage
{
printf STDERR
("Usage: %s inc\n\n",$PROGRAM_NAME);
printf STDERR
("Returns a timestring CCYYMMDDhhmm.ss differing by inc seconds from\n");
printf STDERR
("the current time. Inc may be positive or negative.\n\n");
return(255);
} # Usage

sub Problem
{
my $msg = $_[0];
my $err = $_[1];

printf STDERR ("\n%s: %s (%d)\n",$PROGRAM_NAME,$msg,$err);
return(1);
} # Problem


my $cc = 0;
if ($#ARGV >= 0)
{
if ($ARGV[0] =~ /^[+\-]{0,1}\d+$/)
{
my $inc = $ARGV[0];
my $now = time();
my ($seconds,$minutes,$hours,$mday,$month,$year,$wday,$yday,$isdst)
= localtime($now + $inc);
printf("%04d%02d%02d%02d%02d.%02d\n",$year + 1900,$month + 1,
$mday,$hours,$minutes,$seconds);
}
else
{
$cc = 254;
my $s_err = sprintf("Invalid increment '%s'",$ARGV[0]);
Problem($s_err,$cc);
}
}
else
{
$cc = Usage();
}
exit($cc);




------------------------------------------

If I didn't make any booboo's that should be it and I made it accept positive or negative or zero increments.
If it ain't broke, I can fix that.
Miquel_2
Advisor

Re: time operations

Thank you very much every person who try to help me.

I work with first shell script posted by Keith Bryson beacause when the perl script was posted I was working with it and I don't know how perl scripts works for receive external varibles and and this is the result:

I have a script for rescheduling queued jobs named personal_rescheduling.sh:

Hour=`date +'%H'`
Minute=`date +'%M'`
Second=`date +'%S'`
jobs_dir="/var/spool/cron/atjobs/"
ls -1 $jobs_dir | while read job_name
do
execution_hour=`add_seconds.sh 3 $Hour $Minute $Second`
at -qd -t $execution_hour < $jobs_dir$job_name
if [ $? = 0 ]
then
at -r $job_name
fi
Hour=`add_seconds.sh 3 $Hour $Minute $Second | cut -c 9-10`
Minute=`add_seconds.sh 3 $Hour $Minute $Second | cut -c 11-12`
Second=`add_seconds.sh 3 $Hour $Minute $Second | cut -c 14-15`
done

This script reschedule all queued jobs from now every 3 seconds.

The script used add_seconds.sh is the one posted by Keith Bryson only changed for accept time externaly:

#!/usr/bin/sh
#
# Using: add_seconds.sh seconds_to_add hour minute second
# Return: date and time in at format CCYYMMDDhhmm.ss
#
H=$2
M=$3
SEC=$4
## Lines commented and rewrited beacause when hour, minute or second is 08 or 09 this sentence return error.
#let HOUR=${H}*3600
#let MIN=${M}*60
#let TIME_IN_SECS=${HOUR}+${MIN}+${SEC}
#let TIME_IN_SECS=${TIME_IN_SECS}+$1
#let NEWHOUR=${TIME_IN_SECS}/3600
HOUR=`expr ${H} \* 3600`
MIN=`expr ${M} \* 60`
TIME_IN_SECS=`expr ${HOUR} + ${MIN} + ${SEC}`
TIME_IN_SECS=`expr ${TIME_IN_SECS} + $1`
NEWHOUR=`expr ${TIME_IN_SECS} \/ 3600`
ADJ_HOUR=$NEWHOUR
LOOP=TRUE
while [ $LOOP = "TRUE" ]
do
if [ $ADJ_HOUR -gt 23 ]
then
let ADJ_HOUR=$ADJ_HOUR-24
else
LOOP=FALSE
fi
done

let TMPHOUR=$NEWHOUR*3600
let TMPMIN=${TIME_IN_SECS}-$TMPHOUR
let NEWMIN=${TMPMIN}/60
let TMPSEC=${NEWMIN}*60
let NEWSEC=${TMPMIN}-$TMPSEC
typeset -Z2 HOUR=$ADJ_HOUR
typeset -Z2 MIN=$NEWMIN
typeset -Z2 SEC=$NEWSEC
execution_date=`date +%Y%m%d`
echo "${execution_date}${HOUR}${MIN}.${SEC}"


Miquel_2
Advisor

Re: time operations


I make a script for scheduling jobs like in HP3000 (I recently migrated from this platform), and I called it stream.sh

DIMPSG_HOME="/homes/dimpsg"
ORA_HOME="/homes/oracle"
JOBFI="$DIMPSG_HOME/CONFIG/personal_rescheduling.sh"
if [ "$2" = "-oracle" ]; then
JOBINI="$ORA_HOME/$1.job"
else
JOBINI="$DIMPSG_HOME/JOBS/$1.job"
fi
if [ -f $JOBINI ]; then
cat $JOBINI $JOBFI > arxiutmp
at -qd now < arxiutmp
rm arxiutmp
else
echo "El job que intentes llençar no existeix !!! BUUURROOOOOO. Anónimo"
fi

With this script I append at end of every job the personal_rescheduling.sh script and queue it. Every time a job finishes, it reschedule the rest of queued jobs to grant correct execution order.

Like in HP3000, I make a script to change the job's limit for the queue, obiously I call it limit.sh:

#!/usr/bin/sh
SUDOBIN=/opt/sudo/bin/sudo
COMUSUARI="root"
JLIMIT="$1"
PRI="0"
CONFDIR="/homes/dimpsg/CONFIG"
if [ $1 ]
then
case $1 in
0 ) QUEFEM="stop"
$SUDOBIN -S -i -u $COMUSUARI $HOME/CONFIG/Squeue.sh $QUEFEM $JLIMIT $PRI;;
[1-5] ) QUEFEM="start"
$CONFDIR/personal_rescheduling.sh
$SUDOBIN -S -i -u $COMUSUARI $HOME/CONFIG/Squeue.sh $QUEFEM $JLIMIT $PRI;;
* ) echo "El límit ha d'estar entre 0 i 5 marica!! NO HI HA Mà S LLICà NCIES";;
esac
else
echo "M'INVENTO EL LIMIT QUE VOLS POSAR?, ES QUE....?"
fi

When argument is 0 I stop the queue and when argument between 1 and 5 (max limit for my queue) I reschedule all jobs for grant execution in correct order and start the queue with Squeue.sh script:

#! /usr/bin/sh
#cua a arrancar-parar,nombre de jobs,prioritat
cd /var/adm/cron
OLD_LIMIT=`cat queuedefs | grep "^d."`
NEW_LIMIT=d.$2j$3n2w
sed -e 's/'$OLD_LIMIT'/'$NEW_LIMIT'/' queuedefs >queuedefsold #falla
mv queuedefsold queuedefs
chmod 664 queuedefs
chown bin:bin queuedefs

This script only change the job limit for queue d.

I'm sure there is a so easy way to do all I explain here, but I'm new in HP-UX and I'm so happy this works fine.

Thank you so much!!

Especial thanks to Pere Vidal i
Keith Bryson
Honored Contributor

Re: time operations

Hi Miquel

Glad we could help. Are you finished with this thread now? If so, can you allocate some points to all our responses and close it down. If you need any more help, let us know.

All the best - Keith
Arse-cover at all costs
Miquel_2
Advisor

Re: time operations

Thanks All!!