Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child background Julia processes from R Shiny apps do not stay alive after app is closed

I need to spawn an independent background Julia process from a shiny app that survives once the app is closed.

If the Julia process is called inline: sys::exec_background('nohup', c("julia", "-e sleep(3000)", "&") the child process survives the Shiny app once it get closed.

But if the Julia process is called on a script with just the same sleep(3000) call inside: sys::exec_background('nohup', c("julia", "test.jl", "&") once the Shiny app closes also the Julia process gets killed with error:

signal (2): Interrupt: 2
in expression starting at .../test.jl:1

kevent at /usr/lib/system/libsystem_kernel.dylib (unknown line)
unknown function (ip: 0x0)
Allocations: 2650 (Pool: 2641; Big: 9); GC: 0

Any idea why and any solution on how to have the Julia script process survive the parent app?

Here's the code to reproduce the behaviour. https://gist.github.com/bakaburg1/5d1b5135fb3b4db1a3ca2eb7e8639aa5 Just run the Shiny app and then close it. Only the inline code Julia process survives.

like image 908
Bakaburg Avatar asked Nov 02 '21 17:11

Bakaburg


People also ask

How do I stop a shiny app when closing the browser?

Automatically stop a Shiny app when closing the browser tab. When developing a Shiny app and running the app in the browser (as opposed to inside the RStudio Viewer), it can be annoying that when you close the browser window, the app is still running and you need to manually press “Esc” to kill it.

How do I stop a shiny application in R?

To stop the application you simply interrupt R – you can do this by pressing the Ctrl-C in some R front ends, or the Escape key in RStudio,or by clicking the stop button if your R environment provides one. If you don’t want to block access to the console while running your Shiny application you can also run it in a separate process.

What is the idle time of the shiny app?

I am running the app for only 5 minutes, but when I checked the metrics it shows a running time for about 0.7 hours. I found that there is a default idle time of 15 minutes, which I have changed to 5 minutes (minimum). I also noticed that, even after closing the browser window of the shiny app, it still shows the app as running in my dashboard.

How does shiny detect when a browser reloads the app?

One qualification to this: when a browser reload occurs Shiny explicitly checks the timestamp of the app.R file to see if it needs to be re-sourced. Shiny isn’t aware of other scripts or data files that change, so if you use those files and modify them, a full stop and restart of the application is needed.


1 Answers

There are Unix tools such a nohup and & to detach the forked and process and have it alive without their parent.

This works on my machine and should work on yours:

system("bash -c \"nohup julia -e \\\"println(1);sleep(1000);println(2)\\\" &\"")

Depending on platform you might be able to skip bash -c and just have:

system("nohup julia -e \"println(1);sleep(1000);println(2)\" &")
like image 161
Przemyslaw Szufel Avatar answered Sep 30 '22 06:09

Przemyslaw Szufel