-1

I am trying to write a script which can checking a URL is up and then post the console output in email stating like "200 ok" site is up Scenario 2: Will only trigger email once site is down

What is wrong in the below code

def msgbody='200 OK'

if ((curl -Is https://google.com | head -1) == '$msgbody')
   echo ==========Success
) else (
   echo ==========Fail
  )
  fi    (What is wrong in this code)
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 1
    Hi & welcome to Stack Overflow! This forum is not for questions about technologies or to find somebody doing the work for free (search some freelance service for that). See [How to Ask](https://stackoverflow.com/help/how-to-ask) and [Create Minimal Complete and Verifiable Example](https://stackoverflow.com/help/mcve) for ideas about what kind of questions can be made and [How to Ask](https://stackoverflow.com/help/how-to-ask). – Angel M. Jan 29 '19 at 16:20

2 Answers2

0

You should start with looking at the URL class. http://docs.groovy-lang.org/latest/html/groovy-jdk/java/net/URL.html

That will allow you to read the whole page under a certain URL using getText(). If the page is down, this will throw an exception.

Jenkins should then automatically fail the job. In the Jenkins job definition, you can specify when and how to send a Mail.

EDIT This isn't valid Groovy: (curl -Is https://google.com | head -1). Groovy can't run shell commands or build pipes with | like BASH does. Also, you have to use {} around the then/else blocks after the if, not (). Lastly, there is no fi in Groovy:

 if (condition) {
     ...then...
 } else {
     ...
 }

See Groovy executing shell commands for how to run shell commands from Groovy scripts.

Not that you have to examine the result of exitValue() to determine whether the command failed. 0 is success.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

You can do something like this as a Windows Batch (you need to install curl and configure it in the PATH):

@echo off
curl --silent --output nul --show-error --fail http://domain-or-ip/context
if %ERRORLEVEL% EQU 0 (
   echo ==========Success
) else (
   echo ==========Fail
   exit /b %errorlevel%
)
Nic
  • 327
  • 1
  • 18
  • Thanks Nicolás Santisteban – Aditya Ranjan Jan 30 '19 at 13:51
  • @AdityaRanjan No problem! If this works for you don't forget to mark it as the correct answer :) – Nic Jan 30 '19 at 14:10
  • If you go through the trouble of installing curl, then you can also optimize to send just a HEAD request: https://serverfault.com/questions/340188/using-curl-to-make-a-head-request-with-a-hard-timeout?rq=1 That way, you can check only whether the site is up without download all the data. – Aaron Digulla Jan 30 '19 at 14:28