在Linux中的背景​​用C启动一个进程 [英] Start a process in the background in Linux with C

查看:128
本文介绍了在Linux中的背景​​用C启动一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这里做的东西有点怪异。我需要启动一个进程,logcat中,从将在后台运行,并打印到终端而不采取标准输入控制的守护进程。它是登录状态,这样理想的logcat会记录消息,同时还允许用户输入标准命令和初始化从shell程序。这里是code为守护我有这么远。该计划的logcat,启动并显示日志信息,但因为它似乎是,这个方案采取标准输入的控制,我不能输入任何命令到标准输入。

I am trying to do something a little weird here. I need to start a process, logcat, from a deamon that will run in the background and print to the terminal without taking control of stdin. It is for logging so ideally logcat will print log messages while still allowing the user to input standard commands and initialize programs from the shell. Here is the code for the daemon I have so far. The program, logcat, starts and shows log messages but I cannot enter any commands into stdin as it appears that the program has taken control of stdin.

int main ( int argc, char** argv, char** env )
{
    int fd;
    if ((fd = open("/dev/console", O_RDWR)) < 0) {
        fd = open("/dev/null", O_RDWR);
    }
    printf("THIS IS A TEST\n");
    dup2(1, fd);
    dup2(2, fd);

    pid_t childpid = fork();

    if(childpid == -1) {
        perror("Failed to fork, logcat not starting");
        return 1;
    }

    if(childpid == 0) {
        //this is the child, exec logcat
        setsid();
        int execReturn = execl("/system/bin/logcat", "logcat", (char *) 0);
    } else {
        //this is the parent do nothing
        close(fd);
        return 0;
    }
    close(fd);
     return 0;
}

感谢

推荐答案

借助 logcat的'命令似乎是Android开发 - 这或许可以解释命令的奇位置

The 'logcat' command seems to be for Android development - that might explain the odd location of the command.

这是必须解决的关键操作,以确保您关闭当前的标准输入(终端),并打开的/ dev / null的/ 为输入设备:

The key operation that you must fix is to ensure that you close your current standard input (the terminal) and open /dev/null/ for the input device:

close(0);
if ((fd = open("/dev/null", O_RDONLY)) != 0)
    ...error - failed to open /dev/null!

这意味着你的守护进程的子进程不会从终端读取任何东西。

This means that your daemonized child process will not read anything from the terminal.

我想你想要做的是:


  1. 从一个命令行,这将有标准输入,标准输出和连接到终端标准错误运行你的启动程序。

  2. 您的程序中,要替换的标准输入,因此来自的/ dev / null的

  3. 您想独自离开标准输出 - 你想的logcat 来写入当前标准输出

  4. 您可能要离开标准错误太孤单。

  1. Run your launcher program from a command line, which will have standard input, standard output and standard error connected to 'the terminal'.
  2. Inside your program, you want to replace the standard input so it comes from /dev/null.
  3. You want to leave standard output alone - you want logcat to write to the current standard output.
  4. You probably want to leave standard error alone too.

在诉讼程序的某些时候,你做你的系统守护进程正确(借用@ bstpierre的答案的链接),确保您连接到终端是不是你的控制终端,使中断和挂起发送到终端不会影响你的守护进程。该管道是不是您所设置了简单的 - 你应该处理标准输入和标准留下输出和标准错误不变(而不是改变输出和离开输入不变)

At some point in the proceedings, you do your daemonization properly (borrowing the link from @bstpierre's answer), making sure that the terminal you are connected to is not your controlling terminal, so that interrupts and hangups sent to the terminal don't affect your daemon. The plumbing is simpler than what you have set up - you should deal with standard input and leave standard output and standard error unchanged (instead of changing the outputs and leaving the input unchanged).

现在,你可能想输出去的/ dev /控制台;如果是这样,那么它是合理的修改code打开的/ dev /控制台。然而,这是不合理的依傍的/ dev / null的如果您不能打开的/ dev /控制台 ;你的程序应报告错误和失败(因为在有logcat的书面的/ dev / null的没有意义!)。确保你打开与 O_NOCTTY 标志控制台,以便它不会成为守护进程的控制终端。

Now, you might want the output to go to /dev/console; if so, then it is reasonable to revise the code to open /dev/console. However, it is not reasonable to fall back on /dev/null if you can't open /dev/console; your program should report an error and fail (because there is no point in having logcat writing to /dev/null!). Make sure you open the console with the O_NOCTTY flag so it does not become the controlling terminal for the daemon.

最后的评论我要提出的是:

The final comment I'd make is:


  • 您确定要随机文字出现在你的终端或控制台时,它是在使用其他的东西?

我不太喜欢它当这种情况发生。

I don't much like it when that happens.

另请参阅: SO 958249

这篇关于在Linux中的背景​​用C启动一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆