0

For context, I'm trying to run this in a rails environment.

Basically, I have a rails app, and on a certain controller#action, a job will be spun up with ActiveJob, and in that job is where I want to run a shell script that I wrote.

The shell script just clones another repo containing a rails app into a different directory on the server, installs all required deps, and then starts the server.

So, that means at some point, there will be a parent app and a child app (or children apps)

This is what my perform method looks in the job:

  def perform(resource)
    fork { exec("#{Rails.root.to_s}/my_shell_script.sh #{resource.app_name}") }
  end

and this almost works, but the process isn't separated because fork inherits the parents terminal. So the child app's rails server will get started, but when I go to my terminal window for the parent app where rails s was ran, not only do I see logs for the child app, but when i kill the parent's server with Ctrl-c the child's server also get's killed.

I see two sets of closing messages because both servers are getting killed.

^C- Gracefully stopping, waiting for requests to finish
- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2021-02-06 14:47:46 -0700 ===
- Goodbye!
=== puma shutdown: 2021-02-06 14:47:46 -0700 ===
- Goodbye!
Exiting
Exiting

So my question is how do i separate these? I do not want to see the child app's logs in my server logs and if I kill the parent app's server, I want the child apps to continue to run. Basically, I want them to be completely separated.

What else I've tried

I tried using the system method since it creates a sub shell, but it's still not a completely separate process.

  def perform(resource)
    fork do
      system("#{Rails.root.to_s}/gensite.sh #{resource.app_name}")
    end
  end
Angel Garcia
  • 1,547
  • 1
  • 16
  • 37
  • 2
    Have you tried killing the parent manually with kill in the command-line ? – Vincent Fourmond Feb 06 '21 at 22:09
  • @VincentFourmond Wow, yes I just did and the child app is indeed still running. What does `Ctrl-c` do that `kill` doesn't? And beyond this, is there anyway to still avoid getting logs from my child app in my parent's app server logs? Thanks for responding – Angel Garcia Feb 06 '21 at 22:15
  • 2
    I'm not sure but I think if the child process is connected to the terminal, then both the parent and the child receive SIGINT when you hit Ctrl+C. Killing specifically the parent has no such problem. The problem with the logs is that the log file that was open before forking still stays open to the same place. I'm not familiar enough with rails logging to work around this... – Vincent Fourmond Feb 06 '21 at 22:18
  • 1
    See there too https://stackoverflow.com/questions/8398845/what-is-the-difference-between-ctrl-c-and-sigint – Vincent Fourmond Feb 06 '21 at 22:20
  • What should happen when the server restarts? Should only the main application start automatically again? Or should all other "child" applications that were running start again too? – spickermann Feb 07 '21 at 06:52
  • Have you tried using `nohup`? – simonwo Feb 07 '21 at 09:36
  • @spickermann if the parent server restarts then only the parent server should restart. I want them completely separate – Angel Garcia Feb 07 '21 at 19:05
  • @AngelGarcia: I am sorry it did not think about a re-start of a server _process_ but about the restart of the whole _machine_. – spickermann Feb 07 '21 at 19:12

0 Answers0