How to execute a backend java program?

2,365 views
Skip to first unread message

刘松

unread,
Sep 20, 2016, 2:31:47 PM9/20/16
to Ansible Project
Hi, 
  I use ansible 2.0.0. and I want start a java web program which use spring boot and compile as a jar file, but it always don't work.
  I can execute "nohup java -jar xxx.jar &" on the target machine (the target machine is centos 7),but when I execute through ansible, it don't work.
my playbook is 
- hosts: testservers
  remote_user: root
  tasks:  
   - name: start product
     shell: chdir=/app nohup java -jar /app/product.jar &
     

I think all the method I can , for example, make a .sh file in the target machine, the sh file is
#!/bin/bash
java -jar /app/product.jar > /dev/null 2>&1
and I execute the sh file , it can start .
and the playbook is
shell: chdir=/app nohup ./startproduct.sh &
and I execute the playbook,
the product.jar can not be started yet.

and then I add it to service, the file is
#!/bin/bash
#
#productservice1    start/stop product1
#
# chkconfig: 12345 80 90
# description: start/stop product1
#
### BEGIN INIT INFO
# Description: start/stop product1
### END INIT INFO

function start_product()
{
  nohup java -jar /app/product.jar > /dev/null 2>&1 &
}

function stop_product()
{
    ps -ef | grep /app/product.jar | grep -v grep | awk '{print $2}' | xargs kill -9
}

case "$1" in
    start)
    start_product
    ;;

    stop)
    stop_product
    ;;

    restart)
    stop_product
    start_product
    ;;
    *)
    echo "Usage : start | stop | restart"
    ;;
esac

and I execute the service in target machine, it work!
but I execute it in ansible playbook, it don't work!
service: name=productservice state=started

and I try add
async:45
poll: 0

the playbook still can not start the java program.

I can use playbook kill a process, start tomcat, but can't use it to start a java program( a website, a jar file)

 anybody can help me?

Alexander Taran

unread,
Jan 3, 2017, 12:49:12 PM1/3/17
to Ansible Project
Hello, !
(Sorry can't spell your name).

Try this working example:

- name: Start jenkins
  remote_user: "{{jenkins_owner_user}}"
  shell: cd {{jenkins_home_path_bin}}/ && nohup java -Djenkins.install.runSetupWizard=false -Dhudson.diyChunking=false -DJENKINS_HOME={{jenkins_home_path_data}} -jar ./jenkins.war --httpPort={{jenkins_port}} &
  async: 45
  poll: 0


Reply all
Reply to author
Forward
0 new messages