Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a simple small app that suports cin/cout interaction. But there are times (lots of them) when I do not care about that interaction and just want to run my app in background. So I start it with command like

nohup ./myServer >& /dev/null &

but it eats 100% of one of my cores because nohup sends eof then endl or something like that to my app, my app thinks that this is dull user and proposes him with list of interaction commands and waits for new commad, nohup sends eof then endl again and so on recursively.

So one core gets eaten up with this crappy interaction. I want to start nohup so that it would send no data at all to my app - no eof, endl, etc how to do such thing?

How to make nohup send no data at all to my app after it started it?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

The fix for that is to fix your application.

nohup doesn't send anything to anyone. Your application on the other hand is assuming that stdin is a valid, open file descriptor. It should be checking the return code of whatever primitive it is using to get the data from standard input, and react accordingly if the input stream is found to be closed or invalid.

Since you say cin, I'm going to assume you're coding in C++. This is a safe way of getting strings from std::cin

void get_input()
{
    std::cout << "getting input" << std::endl;

    std::string input;
    while (std::cin >> input) {
        // Good, got something from cin
        std::cout << " * " << input << std::endl;
    }

    std::cout << "out of input loop" << std::endl;
    std::cout << std::cin.eof() << std::endl;
    std::cout << std::cin.bad() << std::endl;
}

This is a bad way of getting user input:

void bad_input()
{
    std::cout << "getting input" << std::endl;

    std::string input;
    while (!std::cin.bad()) {
        std::cin >> input;  // BAD: not checking if the extractor succeeded
        std::cout << input << std::endl;
        // do stuff
    }

    // Not reached if `std::cin` reaches EOF
    std::cout << "out of input loop" << std::endl;
}

Since a stream at eof isn't bad(), this will loop forever once cin reaches OEF. There are many variations for this (both good and bad versions), and similar problems can arise in plain C (and many other languages) for similar reasons.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.