why rake tasks are not executing using and operator?

若如初见. 提交于 2021-01-29 09:21:33

问题


I have a rake task :

task :kill_process do
  system %q(ps -ef | awk '{if($8~"java" || $8~"glassfish" || $8~"ruby" || $8~"god" || $8~"couch"){printf("Killing : %s \n",$2);{system("kill -9 "$2)};}}')
end

This is basically killing processes. And this task is a part of another rake task :

desc "stop the entire system"
task :stop => [...., :kill_process]

There's another task:

desc "start the entire system"
task :start => [....]

When I am doing rake stop && rake start stop task is executed successfully. but rake start is not executing.

If i execute both tasks separately, then it works fine. but not in rake stop && rake start

What will be better to use here exec function or system or any other, please suggest me.

My only requirement is to kill these mentioned processes at the end of rake stop. But also it should not effect other things like rake stop && rake start should work fine.


回答1:


As mentioned in the comments, the exit code is 137 which evaluates to false and therefore the other part of the && does not get executed. The reason for this is probably kill -9.

There are a few options now.

  1. Return 0 from your rake task, something like exit(0)
  2. Don't use kill -9
  3. Create restart command which does execute stop and start but without logically depending on each other (&&).



回答2:


Exit code 137 indicates that a process has received a SIGKILL signal and was thus killed from the outside.

This happens since a Rake task is also executed by Ruby. As such, your stop task is sending a SIGKILL to its own process too (along with all other Ruby processes on the system). Now, since you have specified that you only want to execute the rake start process if the previous process was successful (i.e. had a exit code of 0), your shell doesn't start the rake task.

To quickly fix this, you can instead run rake stop; rake start, i.e run the two processes regardless of their individual exit codes (by default).

However, a better idea would probably to make your stop task more explicit and only kill the specific processes you need rather than everything in sight which looks slightly like a related process. This will likely result in a more stable system overall too when you don't kill potentially unrelated processes all the time.



来源:https://stackoverflow.com/questions/62509528/why-rake-tasks-are-not-executing-using-and-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!