Solved

UNIX shell script question

Posted on 2014-02-20
7
612 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
  • 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

813 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now