1

We have an application that we would like to run a script on just like we do in the console window with access to the applications libraries and context, but we need to run it periodically like a cron job.

While the permanent answer is obviously a Quartz job, we need to the do this before we are able to patch the application.

Is there something available that gives us the same environment as the console-plugin but can be run via command-line or without a UI?

Jim Gough
  • 148
  • 3
  • 12
  • You can run exactly same script from command line passing necessary libs via classpath. Isn't it enough? What libraries and context do You mean? – Opal Apr 08 '14 at 19:16
  • I actually mean access to the context and libraries of my application as it runs. The console plugin allows this as if you are scripting inside of your application as it runs. I was able to solve this problem (sort of) by putting my script in a loop in the Console-plugin window (once the script is executed, it continues to run in the background, even if the page times out). – Jim Gough Apr 09 '14 at 17:25

2 Answers2

2

you can run a console script like the web interface does but just with a curl like this:

curl -F 'code=
class A {
  def name
}

def foo = new A(name: "bar")
println foo.name
' localhost:8080/console/execute

You'll get the response as the console would print below.

mwaisgold
  • 106
  • 2
1

With regard to @mwaisgold 's solution above, I made a couple of quick additions that helped. I added a little bit more to the script to handle authentication, plus the -F flag for curl caused an ambiguous method overloading error with the GroovyShell's evaluate method, so I addressed that by using the -d instead:

#/bin/bash

curl -i -H "Content-type: application/x-www-form-urlencoded" -c cookies.txt -X POST localhost:8080/myapp/j_spring_security_check -d "j_username=admin&j_password=admin"

curl -i -b cookies.txt -d 'code=
int iterations = 0

while (iterations < 10) {
    log.error "********** Console Cron Test ${iterations++} ***********"        
}
log.error "********** Console Cron Test Complete ***********"

' localhost:8080/myapp/console/execute
Jim Gough
  • 148
  • 3
  • 12