Solved

UNIX shell script question

Posted on 2014-02-20
7
623 Views
Last Modified: 2014-04-10
I have a UNIX script that calls a java program and I want to make changes to it so that the script runs the java program only if certain date/time condition is met. The bash script is called by a cronjob every hour or so.

The original script is:
#!/bin/bash
SCRIPTPATH=`dirname $SCRIPT`
java -jar ${SCRIPTPATH}/MYPROG.jar

I want to update it to (pseudo code):

#!/bin/bash
SCRIPTPATH=`dirname $SCRIPT`
if (current GMT/UTC time day-of-week is Sunday and current GMT/UTC time hour-of-day is 00 or 01 or 02 or 03) then do nothing
else {
java -jar ${SCRIPTPATH}/MYPROG.jar
}

The date function on the server returns the UTC time. Could you please suggest how to write the above? The java program will not be called if the current UTC/GMT time is Sunday morning between midnight and 4am. Otherwise the java program will be called.
0
Comment
Question by:toooki
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 375 total points
ID: 39874429
#!/bin/bash
SCRIPTPATH=$(dirname $0)
if [[ $(date -u "+%a") == "Sun" && $(date -u "+%H") -lt 4 ]] ;then
 :
else
 java -jar ${SCRIPTPATH}/MYPROG.jar
fi

I used "$0" instead of $SCRIPT because that variable is not initialized in the piece of code you posted.
$0 contains the name (and path if applicable) by which the script was called.
Nevertheless, if  $SCRIPT is initialized earlier please revert my change.

Further, I replaced the backticks ``with $(  ) for better readability.

"date -u" returns UTC/GMT, regardless of the local timezone.

":" means "no operation" (i. e. "do nothing").
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 125 total points
ID: 39874472
Would it be easier simply not to call the script at the appropriate time?  Have in crontab

0 * * * 1-6 /path/to/script
0 4-23 * * 0 /path to script

First line calls it every hour on Monday to Saturday
Second calls it from 4am to 11pm on Sunday.
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 375 total points
ID: 39874503
You can also put the logic into a single crontab entry:

0 * * * * [[ $(date -u "+\%a") == "Sun" && $(date -u "+\%H") -lt 4 ]] || /path/to/script > /path/to/log 2>&1

Please note that percent signs ( "%" ) must be escaped in crontab ( "\%" )
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:toooki
ID: 39874873
Thanks everyone.

It seems I could use the code:

if [[ $(date -u "+%a") == "Sun" && $(date -u "+%H") -lt 4 ]] ;then
 :
else
 java -jar ${SCRIPTPATH}/MYPROG.jar
fi

Is there any way to avoid the else part of the code above? Is the following same as the above?

if ![[ $(date -u "+%a") == "Sun" && $(date -u "+%H") -lt 4 ]] ;then
java -jar ${SCRIPTPATH}/MYPROG.jar
fi
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 375 total points
ID: 39874899
Nearly.

if ! [[ $(date -u "+%a") == "Sun" && $(date -u "+%H") -lt 4 ]] ;then
java -jar ${SCRIPTPATH}/MYPROG.jar
fi

Note the space between "!" and "[[".
0
 

Author Comment

by:toooki
ID: 39875077
Thanks a lot. I will test and get back to you. Thanks!
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39992356
FYI, an alternative to the"!" would be to Invert the tests and change the && to ||, so
if [[ $(date -u "+%a") != "Sun" || $(date -u "+%H") -ge 4 ]] ;then
    java -jar ${SCRIPTPATH}/MYPROG.jar
fi

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bash file 10 64
Why  my code (program) build with old compiler? 11 108
blank screen when trying to setup Unity on Ubuntu 14.04 9 55
check the file dates in unix 14 24
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question