Solved

UNIX shell script question

Posted on 2014-02-20
7
602 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
Comment Utility
#!/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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:toooki
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a lot. I will test and get back to you. Thanks!
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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.:
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.

744 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

15 Experts available now in Live!

Get 1:1 Help Now