> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/r/run_as_a_daemon_or_service.md).

# Run as a daemon or service

When the "daemon" argument is specified, a fork of the program is created with its STDOUT redirected into the file "foo.txt", and the main process is exited.

```ruby
var block = {
    for n in (1..100) {
        STDOUT.say(n)
        Sys.sleep(0.5)
    }
}

if (ARGV[0] == 'daemon') {
    STDERR.say("Daemon mode")
    STDOUT{:fh} = %f'foo.txt'.open_w(){:fh}
    STDOUT.autoflush(true)
    block.fork
    STDERR.say("Exiting")
    Sys.exit(0)
}

STDERR.say("Normal mode")
block.run
```
