1825944 Members
2664 Online
109690 Solutions
New Discussion

Re: exporting date range

 
Mike Tufariello
Frequent Advisor

exporting date range

Hello all.

I have a script where I am exporting today's date, but I really want to export a date range from today going back 2 days.

This is what I have so far;

export sjj_date=`date "+%Y/%m/%d"`

Thanks,
10 REPLIES 10
A. Clay Stephenson
Acclaimed Contributor

Re: exporting date range

Well it's not obvious that when you specify one date that you have a range but the following will get you 2 days prior to today in YYYY/mm/dd format --- and it will work across month and year boundaries (including leap years):

sjj_date=$(caljd.sh -S "/" -y $(caljd.sh -p 2))
export sjj_date

Invoke the attached script as caljd.sh -u for full usage and examples.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: exporting date range

and here is a Perl equivalent, but the previous example is more powerful and more easily changed:

sjj_date=$(perl -e '($mday,$mon,$year) = (localtime(time() - (2 * 86400))) [3,4,5]; printf("%04d/%02d/%02d",$year + 1900,$mon + 1,$mday)')
export sjj_date

(I think I got all of them "()'s" right.)
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: exporting date range

Hi Mike:

If all you want is 2-days ago, this will do:

# export sjj_date=`perl -MPOSIX=strftime -le 'print strftime "%Y/%m/%d",localtime(time-(86400*2))'`

Regards!

...JRF...
Mike Tufariello
Frequent Advisor

Re: exporting date range

I'm sorry guys. I guess I probably phrased my question wrong. What I am trying to do is display to the screen all jobs that are currently running. I have a script that displays all jobs running for today, only, that were started after midnight. If a job was started prior to midnight and is still running after midnight my script only displays the jobs started after midnight and ignores the job that is still running that was started prior to midnight. Because it only looks for "today's" jobs, not yesterday's. Ideally, I want to display all jobs currently running no matter how long ago they started. Normally we would not let a job run more than 24 hours no matter when it started. So just checking back for the last 24 hours would probably be enough.

Thanks,
A. Clay Stephenson
Acclaimed Contributor

Re: exporting date range

Well, I not guessing that was a very poor way of expressing your problem. I will help to this extent:

Run this command:
UNIX95= ps -e -o comm,pid,etime

Notice that the etime (Elasped time) column changes its format when a process has been running for more than 24 hours. A little pattern matching (awk,perl) should get you the rest of the way. Man ps to understand what this is doing.

I am not going to solve your problem (maybe I just ain't smart enough to figure out how to do it or maybe I figure that it's more important that you learn how to do it, you decide) but anyway I hope that nobody helps more than this either.
If it ain't broke, I can fix that.
Jonathan Fife
Honored Contributor

Re: exporting date range

If you have a script that displays all jobs that have been running for less than a day, your script must be stripping out older jobs to begin with. Take a look at the script and try to figure out where that is happening and modifying it to not do that.

I am assuming "jobs" means "unix processes."
Decay is inherent in all compounded things. Strive on with diligence
Thom Cornwell
Frequent Advisor

Re: exporting date range

$ ps -e -o comm,pid,etime
ps: illegal option -- o
usage: ps [-edaxzflP] [-u ulist] [-g glist] [-p plist] [-t tlist] [-R prmgroup] [-Z psetidlist]

I understand you are trying to format the output, but the command flag -o just does not seem to work for me
Jonathan Fife
Honored Contributor

Re: exporting date range

Thom,

The UNIX95= is an important part of that command. It changes the behavior XPG4, which allows for different ps options and formatting.

The man page for ps should have more information on it.
Decay is inherent in all compounded things. Strive on with diligence
Thom Cornwell
Frequent Advisor

Re: exporting date range

I missed that part, I could see -o in the man page, but the command line thought I lost my beans. Thanx
A. Clay Stephenson
Acclaimed Contributor

Re: exporting date range

The UNIX95=ps ... has the effect of defining the UNIX95 environment variable (as null but just so long as it's defined) for just that command. That triggers the XPG4 behavior of ps. If you have read through the man page down to the EXTERNAL INFLUENCES section, you would have figured this out for yourself ---- plus you wouldn't have made whoever wrote all the documentation stuff mad neither.
If it ain't broke, I can fix that.