shell运行程序,后台运行linux程序,后台运行Linux程序的方法

 2023-09-23 阅读 25 评论 0

摘要:后台运行Linux程序,可以通过crontab设置,这种方法一般用来让一个程序定时运行,也可以通过./test &这样在程序末尾加上一个&使程序在后台运行。编写代码,下面我将参考httpd写一个程序使其运行不占控制台shell运行程序、#include "deta

后台运行Linux程序,可以通过crontab设置,这种方法一般用来让一个程序定时运行,也可以通过./test &这样在程序末尾加上一个&使程序在后台运行。

编写代码,下面我将参考httpd写一个程序使其运行不占控制台

shell运行程序、#include "detach.h"

void detach(void)

{

docker进程?int x;

int pgrp;

chdir("/");

docker里面运行docker、if ((x = fork()) > 0)

exit(0); //exit parent, so we have the child

else if (x == -1) {

nodejs 多进程、perror("fork");

fprintf(stderr, "unable to fork new process\n");

exit(1);

LINUX教程。}

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.

*/

}0b1331709591d260c1c78e86d0c51c18.png

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/87358.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息