[rust-dev] Daemonizing a process

Ilya Dmitrichenko errordeveloper at gmail.com
Tue Jul 15 00:35:00 PDT 2014


Sure, traditional deamons don't have stdio.

You will need to write to a log file and then also care about how that
file is being rotated etc. Traditionally daemons have a  signal handle
that execution reopening of logs... You will also need to write an
init script to actually start it.

What I was saying is that it is not really necessary with a modern
init implementations, such as systemd or upstart. The do take care of
stdio and logging as well as "backgrounding" the service.

Cheers,
-- 
Ilya

On 15 July 2014 07:28, Gulshan Singh <gsingh_2011 at yahoo.com> wrote:
> Alex Crichton helped me figure out a solution:
>
> use std::io::Command;
> use std::os;
> use std::io::timer::sleep;
>
> fn main() {
>     let args = os::args();
>     if args.len() == 1 {
>         let child = Command::new(args.get(0).as_slice())
>             .arg("child")
>             .detached().spawn().unwrap();
>         println!("child: {}", child.id());
>         child.forget();
>     } else {
>         sleep(40000); // In the daemon
>     }
> }
>
> You can use pgrep to confirm the daemon is running. I don't know how to
> print to stdout because the file descriptor is closed (and the daemon
> silently crashes when calling `println`).
>
>
> On Wed, Jul 9, 2014 at 1:00 AM, Ilya Dmitrichenko <errordeveloper at gmail.com>
> wrote:
>>
>> Additionally, I'd like to note that modern best practice is to relay this
>> on the init process itself. With modern init systems, such as systemd or
>> launchctl, this works very nicely and application developer doesn't have to
>> care about daemonisation and logging is also done simply via stdout.
>>
>> On 9 Jul 2014 08:33, "richo" <richo at psych0tik.net> wrote:
>>>
>>> On 08/07/14 23:47 -0700, Gulshan Singh wrote:
>>>>
>>>> Is there currently any way to daemonize a process in Linux? I was using
>>>> the
>>>> `daemon` function in C:
>>>> http://man7.org/linux/man-pages/man3/daemon.3.html.
>>>> I asked in the IRC but I didn't get a response.
>>>
>>>
>>> There's going to be excitement with this, mostly because typically
>>> daemonising involves forking twice to ensure that you're reparented to
>>> init,
>>> which will result in a Really Bad Time if you're using libnative.
>>>
>>> Therefore, it's possible but only if you've not done too much stuff,
>>> which
>>> would be unsafe.
>>>
>>> Seperately though, rust *does* need a coherent story here. I'm just not
>>> at
>>> all sure what it will look like.
>>>
>>> _______________________________________________
>>> Rust-dev mailing list
>>> Rust-dev at mozilla.org
>>> https://mail.mozilla.org/listinfo/rust-dev
>
>


More information about the Rust-dev mailing list