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!