将脚本作为守护程序运行与使用nohup相比有什么影响?
我知道在分叉过程等方面有什么不同,但这对我的脚本有什么影响?
我的问题听起来与此相同,但事实并非如此:
我知道如何 fork() 但不知道如何将进程发送到后台。我的程序应该像一个支持管道和后台进程的简单命令 unix shell 一样工作。我可以做管道和叉,但我不知道如何&像程序的最后一行一样将进程发送到后台:
~>./a.out uname
SunOS
^C
my:~>./a.out uname &
Run Code Online (Sandbox Code Playgroud)
如何实现后台进程?
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#define TIMEOUT (20)
int main(int argc, char *argv[])
{
pid_t pid;
if(argc > 1 && strncmp(argv[1], "-help", strlen(argv[1])) == 0)
{
fprintf(stderr, "Usage: Prog [CommandLineArgs]\n\nRunSafe takes as arguments:\nthe program to be run (Prog) and its command line arguments (CommandLineArgs) (if any)\n\nRunSafe will execute Prog with its …Run Code Online (Sandbox Code Playgroud)