[Haskell-cafe] how to implement daemon start and stop directives?

Magnus Therning magnus at therning.org
Thu Jan 22 07:12:57 EST 2009


On Thu, Jan 22, 2009 at 8:11 AM, Belka <lambda-belka at yandex.ru> wrote:
>
>> You can abstract this pattern:
>>
>> -- runs its argument in an infinite loop, and returns an action that stops
>> the loop
>> daemon :: IO () -> IO (IO ())
>> daemon action = do
>>     stopvar <- atomically $ newTVar False
>>     let run = do
>>           stop <- atomically $ readTVar stopvar
>>           if stop then return () else (action >> run)
>>     forkIO run
>>     return (atomically $ writeTVar stopvar True)
>>
>> TVars are overkill here, actually, an IORef would be just fine, I think.
>>
>> Luke
>
> Thanks, Luke!
>
> Why do you write "return (atomically $ writeTVar stopvar True)" in the end?
>
> Actually, I'm more interested in technical details how to communicate from
> shell with background process - how to send commands to it. Currently
> looking into POSIX libraries hope to find answers there...
> Also, maybe a FIFO-pipe-file would solve my problem. Imagine writing a
> command in it, while one of daemon's thread is locked-while-awaits for
> anything to come out from the other side of the pipe...

I think you should follow Unix "standards" in this case.  So depending
on what you kind of communication you are looking for you have
different options (this is based on my understanding of the Unix way):

 * Signals - for simple control, such as getting the daemon to stop,
reread its configuration, or restart
 * Pipe/Socket - for more complex control such as job control

/M

-- 
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus@therning.org          Jabber: magnus@therning.org
http://therning.org/magnus         identi.ca|twitter: magthe


More information about the Haskell-Cafe mailing list