开发者

Running daemon through rsh

开发者 https://www.devze.com 2022-12-28 06:19 出处:网络
I want to run program as daemon in remote machine in Unix. I have rsh connection and I want the program to be running after disconnection.

I want to run program as daemon in remote machine in Unix. I have rsh connection and I want the program to be running after disconnection.

Suppose I have two programs: util.cpp and forker.cpp.

util.cpp is some utility, for our purpose let it be just infinite root.

util.cpp

int main() {
    while (true) {};
    ret开发者_JS百科urn 0;
}

forker.cpp takes some program and run it in separe process through fork() and execve():

forker.cpp

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char** argv) {
  if (argc != 2) {
    printf("./a.out <program_to_fork>\n");
    exit(1);
  }

  pid_t pid;

  if ((pid = fork()) < 0) {
    perror("fork error.");
    exit(1);
  } else if (!pid) {
    // Child.
    if (execve(argv[1], &(argv[1]), NULL) == -1) {
      perror("execve error.");
      exit(1);
    }
  } else {
    // Parent: do nothing.
  }
  return 0;
}

If I run:

./forker util

forker is finished very quickly, and bash 'is not paused', and util is running as daemon.

But if I run:

scp forker remote_server://some_path/
scp program remote_server://some_path/
rsh remote_server 'cd /some_path; ./forker program'

then it is all the same (i.e. at the remote_sever forker is finishing quickly, util is running) but my bash in local machine is paused. It is waiting for util stopping (I checked it. If util.cpp is returning than it is ok.), but I don't understand why?!

There are two questions:

1) Why is it paused when I run it through rsh?

I am sure that I chose some stupid way to run daemon. So

2) How to run some program as daemon in C/C++ in unix-like platforms.

Tnx!


1) Why is it paused when I run it through rsh?

When you fork a process, the child process has its own copy of the parent's file descriptors. Each of the child's file descriptors refers to the same open file description with the corresponding file descriptor of the parent. After you call fork() you are not closing the standard streams (stdin, stdout, stderr) in the child process before your call to execve() so they are still connected to rsh. It may be the case that rsh will not return as long as any process on the remote server is holding a reference to these streams. You could try closing the standard streams using fclose() before your call to execve() or redirect them when you execute your forker program (i.e. ./forker program >/dev/null 2>/dev/null </dev/null).

2) How to run some program as daemon in C/C++ in unix-like platforms.

According to wikipedia, nohup is most often used to run commands in the background as daemons. There are also several daemon related questions on this site you can refer to for information.

From wikipedia:

nohup is a POSIX command to ignore the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out. The HUP (hangup) signal is by convention the way a terminal warns depending processes of logout.

If your program will always run as a daemon, you can look into the possibility of calling daemon() from within your program. The daemon() convenience function exists in some UNIX systems.

From the daemon(3) man page:

The daemon() function is for programs wishing to detach themselves from the controlling terminal and run in the background as system daemons.

Should this function not exist for you or should there be instances where your program does not run as a daemon, your forker program can also be modified to 'daemonize' your other program.


Without making any changes to your code, you could try something like the following:

rsh remote_server 'cd /some_path; nohup ./forker program >program.out 2>program.err </dev/null &'

0

精彩评论

暂无评论...
验证码 换一张
取 消