1 // This file is written in D programming language
2 /**
3 *   The example demonstrates basic daemonize features. Described
4 *   daemon responds to SIGTERM and SIGHUP signals.
5 *   
6 *   If SIGTERM is received, daemon terminates. If SIGHUP is received,
7 *   daemon prints "Hello World!" message to logg.
8 *
9 *   Daemon will auto-terminate after 5 minutes of running.
10 *
11 *   Copyright: © 2014 Anton Gushcha
12 *   License: Subject to the terms of the MIT license, as written in the included LICENSE file.
13 *   Authors: NCrashed <ncrashed@gmail.com>
14 */
15 module example01;
16 
17 import std.datetime;
18 
19 import dlogg.strict;
20 import daemonize.d;
21 
22 // First you need to describe your daemon via template
23 alias daemon = Daemon!(
24     "DaemonizeExample1", // unique name
25     
26     // Setting associative map signal -> callbacks
27     KeyValueList!(
28         // You can bind same delegate for several signals by Composition template
29         // delegate can take additional argument to know which signal is caught
30         Composition!(Signal.Terminate, Signal.Quit, Signal.Shutdown, Signal.Stop), (logger, signal)
31         {
32             logger.logInfo("Exiting...");
33             return false; // returning false will terminate daemon
34         },
35         Signal.HangUp, (logger)
36         {
37             logger.logInfo("Hello World!");
38             return true; // continue execution
39         }
40     ),
41     
42     // Main function where your code is
43     (logger, shouldExit) {
44         // will stop the daemon in 5 minutes
45         auto time = Clock.currSystemTick + cast(TickDuration)5.dur!"minutes";
46         while(!shouldExit() && time > Clock.currSystemTick) {  }
47         
48         logger.logInfo("Exiting main function!");
49         
50         return 0;
51     }
52 );
53 
54 int main()
55 {
56    	// For windows is important to use absolute path for logging
57    	version(Windows) string logFilePath = "C:\\logfile.log";
58    	else string logFilePath = "logfile.log";
59 	
60     return buildDaemon!daemon.run(new shared StrictLogger(logFilePath)); 
61 }