如何在后台启动脚本并记录到特定文件并完全从ruby中分离?

[英]How to start a script in background with logging to a specific file and completely detaching from ruby?


I have some ruby code (a sidekiq job) which looks like this:

我有一些ruby代码(sidekiq工作),看起来像这样:

module WorkerJobs
  class MyJob < SidekiqWorker

    def perform country
      # Exec, ``, system, Spawn ... whatever?
      # ????
    end

  end
end

I would like to have the ruby code start a command like this (which takes several hours to complete):

我想让ruby代码启动这样的命令(需要几个小时才能完成):

"cd ~/my_dir/production && nohub Rscript --vanilla main.r --country #{country} > /home/deployer/my_dir/shared/log/log_#{country}.log 2>&1 &"

The goal is to get the script started and the sidekiq job to complete without waiting for anything (since the script takes hours to run), so sidekiq can move on and do other jobs. Sidekiq/ruby does not need to know any more details of the state of the job what so ever.

目标是在不等待任何事情的情况下启动脚本并完成sidekiq作业(因为脚本需要数小时才能运行),因此sidekiq可以继续并执行其他工作。 Sidekiq / ruby​​不需要知道工作状态的任何细节。

I tried using system() but that seems to not send the stdout/err to the specified log file for some reason? I tried reading about other ways of executing the script but I can't figure out which method is the right one to use in this case. Again, while the script runs the sidekiq process/thread that started it might stop/start/do-what-ever it should NOT influence the script

我尝试使用system()但似乎没有出于某种原因将stdout / err发送到指定的日志文件?我试着阅读有关执行脚本的其他方法但我无法弄清楚在这种情况下哪种方法是正确的。同样,当脚本运行启动的sidekiq进程/线程时,它可能会停止/启动/执行任何操作它不应该影响脚本

1 个解决方案

#1


0  

Okay so I ended up going with spawn:

好的,所以我最终选择了spawn:

module WorkerJobs
  class MyJob < SidekiqWorker

    def perform country
      logfile =  "/home/deployer/my_dir/shared/log/log_#{country}.log"
      pid = spawn("cd ~/my_dir/production && nohub Rscript --vanilla main.r --country #{country}", [:out, :err] => logfile)
      Process.detach(pid)
    end

  end
end

That worked as intended.

这按预期工作。

Update 1

However now this happens: How to debug R-script suddenly stopping at random point with no error? (related or unrelated I don't know?)

但是现在发生这种情况:如何调试R脚本突然在随机点停止而没有错误? (相关或不相关的我不知道?)

update 2

It actually seems to be related! When sidekiq restarts (get's a kill -15) then it also nukes the process. Strange... Investigating...

它实际上似乎是相关的!当sidekiq重新启动(得到杀死-15)时,它也会破坏这个过程。奇怪......调查......

final update

It turns out, that since sidekiq was started by systemd, systemd was actually the one responsible for nuking any "child" processes (whether they were actually classical child processes or not). This is the default behaviour of systemd. An explanation can be found here: Can't detach child process when main process is started from systemd

事实证明,由于sidekiq是由systemd启动的,因此systemd实际上是负责处理任何“子”进程的人(无论他们是否真的是经典的子进程)。这是systemd的默认行为。可以在此处找到解释:从systemd启动主进程时无法分离子进程

Changing the killmode for system d to process fixed the problem.

更改系统d的killmode进行处理修复了问题。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2017/11/21/6f81c1857f6ec1e30cb643c63772bd45.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告