How to run sbt as daemon?

2020-02-10 15:24发布

问题:

I've tried nohup "sbt run" &

returns : nohup: failed to run command ‘sbt run’: No such file or directory

and tried :

nohup sbt run &
[2] 7897
# nohup: ignoring input and appending output to ‘nohup.out’

When I carriage return expecting process to continue running I receive :

[2]+  Stopped                 nohup sbt run

How to run sbt as a daemon ?

Update :

sbt run </dev/null &
[5] 8961

I think cd up one dir :

# cd ..

[5]+  Stopped                 sbt run < /dev/null  (wd: /home/sum)
(wd now: /home)

So it starts as daemon but if I perform any actions such as changing dir it kills the process ? How to keep process running ?

回答1:

Looks like sbt requested input from your terminal. If it does not really need input (which is probably the case when you run program in background), you can run it like this:

sbt run </dev/null >output-file &

See this answer for details.

EDIT

Ok, now that was a puzzle. Short answer: run sbt as follows:

setsid nohup sbt run &

Rationale:

The reason why sbt stops is arrival of SIGTTOU signal. It is delivered to background process in several cases, which include modifying terminal configuration. This is our case because according to strace -f sbt run &, sbt does a lot of black magic under the hood like this:

[pid 16600] execve("/usr/bin/sh", ["sh", "-c", "stty -g < /dev/tty"], [/* 75 vars */] <unfinished ...>

To work this around, you can run sbt in a different session to detach it from current terminal, so that it won't open /dev/tty and mess with our terminal.



回答2:

This should also work

sbt -Djline.terminal=jline.UnsupportedTerminal run &

source: https://github.com/sbt/sbt/issues/701



回答3:

oleg-andriyanov's answer did not work in my case. (process exited soon after launch)

In a such case, try Mirko Stocker's command written in play ML below for alternative. https://groups.google.com/forum/#!topic/play-framework/ZgjrPgib0-8

# screen -d -m sbt run


回答4:

You can easily use tmux to do this (and persist anything else). A bonus feature is that if you install on a remote server you can persist jobs as "sessions" and reconnect to the same terminal "session". https://www.linode.com/docs/networking/ssh/persistent-terminal-sessions-with-tmux/

1) Launch your sbt job

sbt
run

2) detach with tmux session

ctrl+b (then release)
d

3) Show active tmux sessions (only occurs local tmux)

ctrl + b 
s

4) Show all sessions on remote machine

$ tmux a

5) Attach session

$ tmux attach-session (your-session-number)


标签: linux ubuntu sbt