Run as a daemon or service: Difference between revisions

→‎Tcl: Added implementation
(Added PicoLisp)
(→‎Tcl: Added implementation)
Line 112:
}
}</lang>
 
=={{header|Tcl}}==
Tcl doesn't come with tools for converting the process into a daemon, but can build them easily enough. Here's the BSD daemon function mapped into a Tcl command in a package.
{{libheader|Critcl}}
<lang tcl>package provide daemon 1
package require critcl
 
critcl::ccode {
#include <stdlib.h>
}
critcl::cproc daemon {Tcl_Interp* interp} ok {
if (daemon(0, 0) < 0) {
Tcl_AppendResult(interp, "cannot switch to daemon operation: ",
Tcl_PosixError(interp), NULL);
return TCL_ERROR;
}
return TCL_OK;
}</lang>
These tools can then be used to solve this task:
<lang tcl>### Command line argument parsing
if {$argc < 1} {
puts "usage: $argv0 file ?message...?"
exit 1
} elseif {$argc == 1} {
set filename [lindex $argv 0]
set message "Hi there!"
} else {
set message [join [lassign $argv filename]]
}
 
### Daemonize
package require daemon
daemon
close stdout; open $filename ;# Redirects stdout!
 
### Print the message to the file every second until killed
proc every {ms body} {eval $body; after $ms [info level 0]}
every 1000 {puts "[clock format [clock seconds]]: $message"}
vwait forever</lang>
On Windows, there is a commercial extension to Tcl which allows a script to be installed as a service. Such a script would be much like the one above, but without the daemonization section as that has become a property of the runtime.
Anonymous user