Running daemon via rsh

I want to run the program as a daemon on a remote machine on Unix. I have an rsh connection and I want the program to start after disconnecting.

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

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

util.cpp

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

forker.cpp takes some program and runs it in the separe process via 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 exits very quickly, but bash 'does not pause, and the utility works like a 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' 

it’s all the same (that is, remote_sever runs quickly on the remote_file, the utility runs), but my bash on the local computer is paused. It waits for usage to stop (I checked it. If util.cpp returns, that's fine.), But I don't understand why ?!

There are two questions:

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

I'm sure I chose some stupid way to start the daemon. So

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

Tnx!

+4
source share
1 answer

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

When a process skews, the child process has its own copy of the descriptors of the parent file. Each of the child file descriptors refers to the same open file description with the corresponding parent file descriptor. After calling fork() you do not close the standard streams ( stdin , stdout , stderr ) in the child process until you call execve() so that they still connect to rsh. It may be that rsh will not return as long as any process on the remote server holds a link to these threads. You can try to close the standard threads using fclose() before calling execve() or redirect them when you run your forker program (i.e. ./forker program >/dev/null 2>/dev/null </dev/null ) .

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

According to wikipedia , nohup most often used to run commands in the background as daemons. On this site there are several questions related to the demon that you can contact for information.

From wikipedia :

nohup is a POSIX command to ignore the HUP (hangup) signal, allowing the command to continue to work after the user logs out of the system that issues the command. The HUP (hang) signal is consistent with how the terminal warns of dependent logout processes.

If your program will always work as a daemon, you can explore the possibility of calling daemon() from your program. The handy daemon() function exists on some UNIX systems.

From the daemon (3) man page :

The daemon () function is intended for programs that want to disconnect from the control terminal and run in the background as system daemons.

If this function does not exist for you or there should be instances in which your program does not start as a daemon, your forker program can also be modified to "demonize" your other program.


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

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

+2
source

Source: https://habr.com/ru/post/1308250/


All Articles