Solved

UNIX shell script question

Posted on 2014-02-20
7
620 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to install Ubuntu 16 in DELL venue 8 pro 20 299
Linux Real Time Memory on Distinct 24 Cycle 7 29
Internal CA server 6 47
parallel rsync issues with Ubuntu 1 27
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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

829 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