# Keeping Julia alive while running a web server in the background

**URL:** <https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422>\
**Category:** General Usage\
**Created:** [January 16, 2018, 11:32pm UTC](https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422 "2018-01-16T23:32:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![NickNack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicknack/32/1792_2.png) [@NickNack](https://discourse.julialang.org/u/NickNack)\
**Post date:** [January 16, 2018, 11:32pm UTC](https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422/1 "2018-01-16T23:32:41Z")

</div>

I’m using [Mux.jl](https://github.com/JuliaWeb/Mux.jl) to run a web server. My code is in a module with the following structure:

```julia
module MyApp

using Mux

# all app functions here

function startserver()
    # setup routes here (using the functions above)
    @app webserver = (routes...)
    serve(webserver)
end
end

```

Everything works perfectly when I run it from the REPL with `using MyApp; startserver()`. The server runs in the background within that session. In other words, I can type commands and do other stuff in the REPL while the server is online responding to all requests I make in a browser. When I exit Julia, the server naturally goes down. So far so good.

Now I want to SSH into my computer and start the web server, and keep the server running after I exit the terminal window. I thought something like this at the Linux command line would work:

```julia
nohup julia -e 'using MyApp; startserver()' &

```

However, the process immediately terminates after starting the server (printing `[1] Done`) and the server goes down. Same result when I put those two commands in a script and call it. How can I keep the server alive?

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [January 17, 2018, 12:43am UTC](https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422/2 "2018-01-17T00:43:30Z")

</div>

Linux answer here, not Julia specific. Use GNU Screen (or tmux), create a session, run the command you want, detach the screen. Involves learning how to use screen, but it’s useful for other things too - I used to have an IRC client running this way. There may be other answers.

---

<div class="post-metadata">

**Author:** ![NaOH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naoh/32/5632_2.png) [@NaOH](https://discourse.julialang.org/u/NaOH)\
**Post date:** [January 17, 2018, 1:30am UTC](https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422/3 "2018-01-17T01:30:28Z")

</div>

Based on:

[https://github.com/JuliaLang/IJulia.jl/blob/48f5bafa06d3efb919743b2a8567ae911b8fd629/src/IJulia.jl#L105-L127](https://github.com/JuliaLang/IJulia.jl/blob/48f5bafa06d3efb919743b2a8567ae911b8fd629/src/IJulia.jl#L105-L127)

You could try something like:

```julia
julia> p = spawn(Cmd(`julia -e 'using MyApp; startserver()'`, detach=true))
Process(`julia -e 'using MyApp; startserver()'`, ProcessRunning)

```

---

<div class="post-metadata">

**Author:** ![jballanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jballanc/32/328_2.png) [@jballanc](https://discourse.julialang.org/u/jballanc)\
**Post date:** [January 17, 2018, 8:21am UTC](https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422/4 "2018-01-17T08:21:14Z")

</div>

It appears that `Mux.jl` is choosing to run your server asynchronously by default, with no option to run synchronously (see [here](https://github.com/JuliaWeb/Mux.jl/blob/master/src/server.jl#L45)).

This seems like bad practice, in general, and at the very least should be something configurable. If your application is _just_ a web server, the current best practice is to have the main server request/response loop run in the main thread, and then use an OS-level process manager (such as [Upstart](http://upstart.ubuntu.com/) or [systemd](https://freedesktop.org/wiki/Software/systemd/) in Linux, or the classic [rc](https://www.freebsd.org/doc/en/books/handbook/configtuning-rcd.html) system used by FreeBSD) to daemonize your server. The advantage to using such tools is that it becomes (relatively) trivial to start, stop, and restart the server, gather logs, ensure the server stays up, and have the server start on boot.

The “old-n-busted” way to write your own daemon involves the dreaded “double-fork” (see [this Stack Overflow answer](https://stackoverflow.com/questions/881388/what-is-the-reason-for-performing-a-double-fork-when-creating-a-daemon#5386753) for a brief explanation as to why). Don’t do this. Really.

To solve your immediate problem, you have two choices:

1. Pause the main thread after starting your server. Non-ideally, this could just be `while(true); sleep(1); end` (this solution is simple, but this unnecessarily consumes processor resources). Alternatively, see the documentation for `wait` for some other ideas. For example, I think creating a `Condition` that you `wait` on, and just never trigger, might be sufficient.
2. Reproduce `HttpServer.jl`’s `run` command, dropping the `@async` qualifier. One caveat, having taken the briefest of looks at the underlying code in `HttpServer.jl` and `Base`, is that it seems like even dropping `run`’s `@async` might not be enough to run the server synchronously.

---

<div class="post-metadata">

**Author:** ![NickNack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicknack/32/1792_2.png) [@NickNack](https://discourse.julialang.org/u/NickNack)\
**Post date:** [January 17, 2018, 11:36am UTC](https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422/5 "2018-01-17T11:36:56Z")

</div>

Three completely different responses, all useful. Thanks guys! Based on @jballanc’s last suggestion I came up with this simple hack:

```julia
nohup julia -e 'using MyApp; @sync startserver()' &

```

I believe that should be enough to force the server to run synchronously, despite all the `@async` calls in the underlying packages. It seems to be running as intended, with negligible CPU usage when not responding to a request.

Am I correct? Is this a very bad idea in some other way?

---

<div class="post-metadata">

**Author:** ![jballanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jballanc/32/328_2.png) [@jballanc](https://discourse.julialang.org/u/jballanc)\
**Post date:** [January 17, 2018, 11:49am UTC](https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422/6 "2018-01-17T11:49:39Z")

</div>

> [@NickNack](#):
>
> Am I correct? Is this a very bad idea in some other way?

Heh…no, actually I didn’t realize that a top-level `@sync` macro would override any underlying `@async` calls in included packages, but if this works I see no problem with it. You’ve got a server running on a main thread and you didn’t even have to spin anything!

---

<div class="post-metadata">

**Author:** ![kimran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimran/32/7782_2.png) [@kimran](https://discourse.julialang.org/u/kimran)\
**Post date:** [March 28, 2018, 2:55pm UTC](https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422/7 "2018-03-28T14:55:40Z")

</div>

This solved another general problem for me

I was going open a thread asking how to make application loops

Say I have several tasks (via `@async`) running in my main method, the only way I saw so far to keep this alive while NOT running in the REPL was to add a `while true;sleep(1);end` loop, which seems such bad practise

Running `@sync main()` has solved this - the script keeps running (while all tasks run) without the `while true...end`

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 30, 2019, 1:19pm UTC](https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422/8 "2019-12-30T13:19:30Z")

</div>

@sync did not work for my use case. However, adding `timedwait(()->false, Inf; pollint=100.0)` to the end of my script worked quite well without consuming any resources.

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [December 30, 2019, 1:33pm UTC](https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422/9 "2019-12-30T13:33:32Z")

</div>

Much simpler and (IMO) more idiomatic: `wait(Condition())`

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [December 30, 2019, 1:36pm UTC](https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422/10 "2019-12-30T13:36:33Z")

</div>

Pretty sure you can also run julia in Interactive move with the `-i` cmd line arg. This will leave a background REPL open though, so that could be a security risk.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 30, 2019, 2:48pm UTC](https://discourse.julialang.org/t/keeping-julia-alive-while-running-a-web-server-in-the-background/8422/11 "2019-12-30T14:48:18Z")

</div>

Thanks. I was not aware that I could pass `Condition()` to wait without arguments.
