歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 後台運行Linux程序的方法

後台運行Linux程序的方法

日期:2017/2/28 16:25:11   编辑:Linux教程

後台運行Linux程序,可以通過crontab設置,這種方法一般用來讓一個程序定時運行,也可以通過./test &這樣在程序末尾加上一個&使程序在後台運行。

編寫代碼,下面我將參考httpd寫一個程序使其運行不占控制台

#include "detach.h"
void detach(void)
{
int x;
int pgrp;

chdir("/");

if ((x = fork()) > 0)
exit(0); //exit parent, so we have the child
else if (x == -1) {
perror("fork");
fprintf(stderr, "unable to fork new process\n");
exit(1);
}

raise(SIGSTOP);
/* setsid - run a program in a new session*/
if ((pgrp = setsid()) == -1) {
perror("setsid");
fprintf(stderr, "setsid failed\n");
exit(1);
}

/* close out the standard file descriptors */
if (freopen("/dev/null", "r", stdin) == NULL) {
fprintf(stderr, "unable to replace stdin with /dev/null:\n");
/* continue anyhow -- note we can't close out descriptor 0 because we
* have nothing to replace it with, and if we didn't have a descriptor
* 0 the next file would be created with that value ... leading to
* havoc.
*/
}
if (freopen("/dev/null", "w", stdout) == NULL) {
fprintf(stderr, "unable to replace stdout with /dev/null: \n");
}
/* stderr is a tricky one, we really want it to be the error_log,
* but we haven't opened that yet. So leave it alone for now and it'll
* be reopened moments later.
*/
}

Copyright © Linux教程網 All Rights Reserved