Advertisement

Can a background process simply vanish?

Started by August 08, 2008 08:49 AM
6 comments, last by HuntsMan 16 years ago
Hello. I have two unrelated executables that I started like this: 1) opened a ssh shell 2) Started the two applications $ mfsrv & $ chatsrv & 3) Exited the shell $ exit Today when I logged in to my server both were gone! Both programs are written by myself. chatsrv runs an endless loop and never terminates programmatically. mfsrv terminates programmatically but it prints a message in the log. No message was there. I made the following checks: 1) Checked core dumps I found no cores. My ulimit is set to 'unlimited', and my system is set to dump cores in the cwd. None of these processes ever changes the cwd. I wouldn't be suprised if one of them had cored. But both! They are totally unrelated. 2) Checked if the system had rebooted last boot it didn't reboot 3) Check if someone else had accessed my server last -x I was the last one to access the server. What is going on? Normally 'ps -eaf' prints this, when the 2 processes are running: UID PID PPID C STIME TTY TIME CMD mfoot 7937 1 0 13:17 ? 00:00:00 ./mfsrv mfoot 8012 1 0 13:21 ? 00:00:00 ./chatsrv Is the kernel terminating my processes?
Quote: Original post by hiigara
Is the kernel terminating my processes?

No, you are terminating your processes.

When a parent process (process group leader) terminates, so do its children. You can't just backgrund a job from the shell and log out. You need to set the jobs into their own process groups.

One way to do this is to write your process as a daemon. You need to detach from controlling terminals and create a new process group (fork/fork/setsid etc).

A simpler way is to use the nohup command.
  $ nohup mfsrv >mfserv.log 2>&1 </dev/null &  $ nohup chatsrv >chatserv.log 2>&1 </dev/null &

Stephen M. Webb
Professional Free Software Developer

Advertisement
Quote: Original post by Bregma
No, you are terminating your processes.]

No, I am not. Both servers are accessible through a client I have on my windows PC. And that's how I have been starting the servers for a year now. It works in both suse linux and ubuntu. I just exit the shell and the servers keep running.


Quote:
One way to do this is to write your process as a daemon. You need to detach from controlling terminals and create a new process group (fork/fork/setsid etc).

A simpler way is to use the nohup command.
  $ nohup mfsrv >mfserv.log 2>&1 </dev/null &  $ nohup chatsrv >chatserv.log 2>&1 </dev/null &

Thanks I will use nohup, creating deamons with system calls is hard work.
But still I would like to know exactly why my servers were killed.
Quote: It works in both suse linux and ubuntu. I just exit the shell and the servers keep running.


It probably doesn't. What most likely happens on Suse and Ubuntu is that your ssh session gets stuck. I.e. you disconnected but the remote session doesn't really close. After a while you will have many dead tty processes eating up resources.

Instead of nohup, you can also look at the start-stop-daemon command. With that you can use regular applications as daemons. They run separately and will not terminate when you close your session.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Quote: Original post by Sander
Instead of nohup, you can also look at the start-stop-daemon command. With that you can use regular applications as daemons. They run separately and will not terminate when you close your session.

Keep in mind start-stop-daemon is a Debian thang. It's not generally available in Red Hat and derivatives. The LSB standard assumes that a daemon process is a daemon, so there is no mandated cross-platform tool to wrap a non-daemon in a portable way. Except for maybe nohup.

Stephen M. Webb
Professional Free Software Developer

Quote: Original post by Bregma
Keep in mind start-stop-daemon is a Debian thang.


Ah yes, of course. All I ever use is Debian-based distros :-) Though according to Google, Gentoo seems to have it. Fedora/RedHat/CentOS have "daemon" which is supposed to be similar.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Advertisement
Type "man daemon"

DAEMON(3)                  Linux Programmer’s Manual                 DAEMON(3)NAME       daemon - run in the backgroundSYNOPSIS       #include <unistd.h>       int daemon(int nochdir, int noclose);...DESCRIPTION       The daemon() function is for programs wishing to detach themselves from       the controlling terminal and run in the background as system daemons.


It's not POSIX, but it's in any remotely recent glibc.
You can use screen also to run those processes.

This topic is closed to new replies.

Advertisement