通过 rsh 运行守护进程

发布于 2024-08-30 23:53:18 字数 1519 浏览 7 评论 0原文

我想在 Unix 的远程机器上将程序作为守护进程运行。我有 rsh 连接,我希望程序在断开连接后运行。

假设我有两个程序:util.cpp 和 forker.cpp。

util.cpp 是一些实用程序,出于我们的目的,让它只是无限根。

util.cpp

int main() {
    while (true) {};
    return 0;
}

forker.cpp 获取一些程序并通过 fork() 和 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;
}

如果我运行:

./forker util

forker 很快就完成了,并且 bash “未暂停”,并且 util 作为守护进程运行。

但是如果我运行:

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

那么它都是一样的(即在remote_sever forker 很快完成,util 正在运行)但是我在本地计算机中的bash 暂停了。 它正在等待 util 停止(我检查过。如果 util.cpp 返回则没问题。),但我不明白为什么?!

有两个问题:

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

我确信我选择了一些愚蠢的方式来运行守护进程。所以

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

Tnx!

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) {};
    return 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恰似旧人归 2024-09-06 23:53:18

1) 为什么当我通过 rsh 运行它时它会暂停?

当您分叉一个进程时,子进程拥有自己的父进程文件描述符副本。子级的每个文件描述符都引用与父级相应的文件描述符相同的打开文件描述。调用 fork() 后,您不会关闭标准流(stdinstdoutstderr)调用 execve() 之前的子进程,因此它们仍然连接到 rsh。只要远程服务器上的任何进程持有对这些流的引用,rsh 就可能不会返回。您可以尝试使用 fclose() 在调用 execve() 之前或在执行 forker 程序时重定向它们(即 ./forker program >/dev/null 2>/dev/null )。

2) 如何在类 UNIX 平台上以 C/C++ 方式运行某些程序作为守护进程。

根据维基百科< code>nohup 最常用于在后台作为守护进程运行命令。此站点上还有几个与守护程序相关的问题,您可以参考以获取信息。

来自维基百科

nohup 是一个 POSIX 命令,用于忽略 HUP(挂断)信号,使命令在发出该命令的用户注销后继续运行。按照惯例,HUP(挂断)信号是终端根据注销进程发出警告的方式。

如果您的程序始终作为守护进程运行,您可以研究从程序内调用 daemon() 的可能性。 daemon() 便利函数存在于某些 UNIX 系统中。

daemon(3) 手册页

daemon() 函数适用于希望将自己与控制终端分离并作为系统守护进程在后台运行的程序。

如果您不存在此功能,或者您的程序不作为守护程序运行,您的 forker 程序也可以修改为“守护程序”您的其他程序。


在不对代码进行任何更改的情况下,您可以尝试类似以下内容:

rsh remote_server 'cd /some_path; nohup ./forker 程序 >program.out 2>program.err

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 &'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文