Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I ssh into a virtual server and start up a web server. Everything runs as expected. But when I close my terminal on my laptop, the process dies on the virtual server. How do I fix this?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

You can use disown, it is a bash builtin:

disown [-ar] [-h] [jobspec ...]

Without options, each jobspec is removed from the table of active jobs. If the -h option is given, each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If no jobspec is present, and neither the -a nor the -r option is supplied, the current job is used. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r option without a jobspec argument restricts operation to running jobs. The return value is 0 unless a jobspec does not specify a valid job.

Try this:

$ <your command> &
$ disown

First, make your command run in background by typing <your command> &, then use disown, it will make your command keep running even if your ssh session is disconnected.

IMHO, you should use a tool to control your service, like supervisord or writing your own init script.

share|improve this answer
    
what does the ampersand do in the $command & line? Do I type the ampersand? –  akh2103 Mar 8 at 17:38
    
Yes, type <your command> & make your program running in background. –  Gnouc Mar 8 at 17:41
    
I should add, that stopping that service is a bit difficult with ps and killing the right PID. It might be a good idea to save that PID with echo $! > server.pid, so it's easier to stop a process just by using that PID. –  polemon Mar 8 at 17:50
    
Why is it difficult to stop with ps? –  Gnouc Mar 8 at 17:53

Assuming you are working with httpd on a systemd based system -

When logged into the server, you should run these commands:

sudo systemctl enable httpd.service
sudo systemctl restart httpd.service

You can check to see if it is running on subsequent logins by running this command:

systemctl status httpd.service
share|improve this answer

You need to take care of two things:

  • Redirecting the standard input/output streams to/from nowhere.

  • Forking the process into the background reparented by init.

The "reparented by init"1 bit is the most crucial part, since otherwise the process will receive a HUP when you log out. The nohup command can block the HUP, and the now orphan process will then be adopted by init, but a better solution is simply to use setsid, which immediately starts the process in a new process group, with init as its parent:

setsid myprog < /dev/zero 2>&1&> /dev/null

< /dev/zero redirects stdin; this may or may not matter depending on the nature of myprog. 2>&1&> /dev/null redirects output so that you don't get any junk on your terminal before you log out.


1. Init is the first process started on the system (by the kernel) and the only one that cannot die. The PID of init is always 1. Note that some systems use an init daemon with a different executable name, you can identify it with ps -p 1.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.