简体   繁体   中英

Boost.Log and creating daemons; `fork` disallowed?

I need to add a command-line option to my application saying that it is to be run as a deamon .

However, I am also using boost logging library to keep logs of this application, and I found out that boost logging does not support forking.

This seems to prevent me from forking, and as such I cannot create a daemon .

  • Is it possible to bypass this problem, or;
  • can I create a daemon process without forking?

The fork s in a daemon play an important role for the daemon to work as expected as mentioned in the answers to this question .

If the only problems are due to multiple processes logging the fork should not be a problem since you don't have to log befor you have done the forks. Besides the parent processes of these forks are going to terminate anyway.

If termination of the parents are problematic, you could maybe postpone initialization of the boost logging until after the second fork .

If boost logging is always initialized before main the solution might need to be to make sure that the fork s happen even before that, that is to manage to make the code run befor boost logging initialization - which will need a implementation specific solution.

For an implementation independent (other than posix support) solution for the worst case scenario is to use execl to make sure that the actual daemon doesn't fork, that you in effect use one program that does the daemonizing thing which don't use boost logging and one program that's the proper daemon program. If it's not a big problem with fork if you don't use the logging fascility (after the fork) you could even do this with one single executable and differ the behaviour from command line switches. In pseudo code:

int main() {
    parse_command_line();

    if( no_daemonize_flag() )
        run_daemon()
    else {
        daemonize();
        execl("/path/to/daemon", "/path/to/daemon", "--no-daemonize", ...other flags..., NULL);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM