1

I have a bash script on my server which fetches master branch from github, builds it and deploys artifacts to the tomcat. I decided to write a github web URL hook which will call this sh. As my server is running Java, I'm using groovy. I writed a test script test.sh in /home/madhead/scripts:

echo "SHELL"
touch /home/madhead/test_`date +%d_%m_%Y_%H_%M_%S` # To see if script is actually called

Environment variable SCRIPTS is set to /home/madhead/scripts in /home/madhead/.bashrc. In my groovlet I have

println "GROOVY"
println '$SCRIPTS/test.sh'.execute().text
println `whoami'.execute().text` //Prints madhead
println `env'.execute().text` //Prints all environment variables for madhead, SHELL is /bin/bash and SCRIPTS is /home/madhead/scripts in this output.

"GROOVY" is printend in html, but no "SHELL" and no test files are created when i calling groovlet. So, the script is not called. I tried

println '/home/madhead/scripts/test.sh'.execute().text

in groovlet with no effect. How do i call bash script from groovy/java? Also, println 'echo test'.execute().text prints test to html, but println 'echo $SCRIPTS'.execute().text does not print anything. Why?

5
  • 2
    did you try adding a shebang to the script? Commented Aug 14, 2012 at 17:38
  • You are rigth, there were no shebang in shell script. But when i added it nothing changed. Commented Aug 14, 2012 at 21:30
  • UPD. Also, it ocurred that i need to call waitFor() on process. When i don't do this shell script is not not executing. Commented Aug 14, 2012 at 21:40
  • 1
    Are you sure that your shell script is executable? chmod +x test.sh Commented Aug 15, 2012 at 6:51
  • Sure, it is execuable, i can call it from shell Commented Aug 15, 2012 at 7:22

1 Answer 1

2

Maybe to get the environment variable you need to:

def env = System.getenv()
println env.SCRIPTS

In my box this guy works:

groovy -e ' def env = System.getenv(); println env.JAVA_HOME '
Sign up to request clarification or add additional context in comments.

2 Comments

There is no SCRIPTS variable in System.getenv(). I've added the shebang to the shell script and calling it by absolute path.
Yes, i understood the issue, i wrote #!/bin/sh, while setting SCRIPTS in bash config. Now i call the shell script with def p = "${System.getenv().SCRIPTS}/test.sh".execute(); p.waitFor(); println p.text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.