2

I have a web app on a freebsd server. An app is a single executable file, with a built-in webserver. My default shell is zsh.

When I'm running it as "./my_website &" and close a connection, it terminates. However, when I switch to bash by "/usr/local/bin/bash" and run my website the same way "./my_website &", then exit bash and close a connection, my website keeps working -- exactly what I need.

What's going on here -- why does it keep running in bash after I close a connection to my server?

Is it a good practice to run an app in background this way? If not, what's the proper way to run my website so that it won't terminate after I disconnect?

Dari
  • 121
  • 2

4 Answers4

1

Or you may run it in screen:

screen -dm bash -c '/path/to/your/program'
Jaroslav Kucera
  • 1,435
  • 10
  • 16
0

The proper way is to turn your app into a system service and add yourapp_enable="YES" into /etc/rc.conf to [re]start it automatically. See Practical rc.d scripting in BSD article for example.

arrowd
  • 319
  • 1
  • 8
0

You need to use nohup command

nohup ./my_website &

It will let your process run after parent process dies/exits regardless of shell.

user1700494
  • 1,642
  • 2
  • 11
  • 20
0

It depends on what is you goal. If you need to run this webserver for a short time (development/test), you can use screen/tmux and leave it in open session.

If you want to run this application in production it is better to write startup script. If this application can't demonize them-self you can use daemon(8). I recommend to read Practical rc.d scripting in BSD, also you can find a lot of examples in the ports tree. To find rc-scripts with daemon you can use command find /usr/ports/ -depth 4 -name *.in | xargs fgrep -l /usr/sbin/daemon

citrin
  • 469
  • 2
  • 5
  • but why does it keep running in bash? – Dari Sep 22 '17 at 00:08
  • **If this application can't demonize them-self** --> how can I check this? – Dari Sep 22 '17 at 00:09
  • 1. May be bash doesn't kill background jobs on logout. Anyway you application will be killed if it try to write something in stdout/stderr (you can redirect them to /dev/null). 2. Many application have an option to run in background (demonize), check man page (if it exists). – citrin Sep 23 '17 at 00:39