Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

how to run a java program at background from chef recipe

I am a chef rookie. I want to create a recipe to run a jar at background.

bash 'run_jar' do
    code <<-EOH
    wget https://github.com/kiwiwin/jar-repo/releases/download/kiwi/helloworld-1.0.jar -O hello.jar
    java -jar hello.jar &
    EOH
end

The helloworld-1.0.jar is a program first print "Hello World", then execute a while(true) loop.

I expect when I login to the chef-client machine. It should indicate there is a jar running using "jps" command. But there is no such jar running.

And I can see the hello.jar is downloaded which indicates the code block has been executed already.

What's wrong with this recipe?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are best advised to configure your code to run as a service. Several wrappers available, for example:

Once this is done you can configure chef to manage the new service:

service "myapp_service" do
  supports :status => true, :restart => true
  start_command "/usr/lib/myapp/bin/myapp start"
  restart_command "/usr/lib/myapp/bin/myapp restart"
  status_command "/usr/lib/myapp/bin/myapp status"
  action [ :enable, :start ]
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...