Run as a daemon or service: Difference between revisions

m
syntax highlighting fixup automation
(Added Wren)
m (syntax highlighting fixup automation)
Line 11:
==={{header|FreeBASIC}}===
{{trans|QB64}}
<langsyntaxhighlight lang="freebasic">ScreenLock()
Open "c:\Users\Default\Documents\myDaemon.log" For Append As #1
'Open "myDaemon.log" For Append As #1 'Ruta de archivo relativa para la salida // Relative file path for the output
Line 22:
Close #1 'Cierra el archivo previamente abierto para escritura // Close the file previously open for writing
ScreenUnlock()
End</langsyntaxhighlight>
 
==={{header|QB64}}===
<langsyntaxhighlight lang="freebasic">'Metacommands and Statements for closing console or window
'' The following line is double commented ('') as some metacommands still execute even when singularly commented (')
''$Console 'Closes the console but does not close the main window, used for whole program run
Line 40:
Next i%
Close #1 'Closes the file previously opened for writing
System</langsyntaxhighlight>
{{out}}
<pre>09:02:23 - Running
Line 54:
The task also wants to redirect stdout. This program does so with [http://netbsd.gw.com/cgi-bin/man-cgi?dup2+2+NetBSD-current dup2(2)]. Had we wanted to directly write to a file, we could open the file with <code>file = fopen(argv[1], "a")</code>, and write to ''file'' instead of ''stdout''.
 
<langsyntaxhighlight lang="c">#include <err.h>
#include <errno.h>
#include <fcntl.h>
Line 105:
sleep(1); /* Can wake early or drift late. */
}
}</langsyntaxhighlight>
 
<pre>$ make dumper
Line 123:
{{libheader|go-daemon}}
{{works with|Ubuntu 16.04}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 174:
 
work()
}</langsyntaxhighlight>
 
{{out}}
Line 196:
{{trans|Python}}
This is a direct translation of the Python Posix version.
<langsyntaxhighlight Nimlang="nim">import os, posix, strutils
 
let pid = fork()
Line 222:
while now() < t + initDuration(seconds = 10):
echo "timer running, $# seconds".format((now() - t).inSeconds)
sleep(1000)</langsyntaxhighlight>
 
{{out}}
Line 242:
=={{header|Perl}}==
First off, start with a standalone script and let's call it "count.pl"
<langsyntaxhighlight lang="perl"># 20211217 Perl programming solution
 
use strict;
Line 261:
print $fh "Sheep number ",$count++," just leaped over the fence ..\n";
$fh->flush();
}</langsyntaxhighlight>
Make sure it works and then we can control/run it in daemon mode with another script
<langsyntaxhighlight lang="perl">use warnings;
use strict;
 
Line 272:
program => '/home/hkdtam/count.pl',
pid_file => '/tmp/counter.pid',
)->run;</langsyntaxhighlight>
{{out}}
<pre>
Line 318:
Of course there is nothing at all to stop fn from being set to 1 for stdout, or 2 for stderr.<br>
It is also strongly against the core philosophy of Phix to have or permit anything that would somehow make printf(1,x) do something it does not look like it should be doing, especially given printf(fn,x) has no such ambiguity. Not having to change your code may sound dandy, not being able to tell what your code is doing by looking at it, isn't.
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 327:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- [close(1) quietly does nothing]</span>
<!--</langsyntaxhighlight>-->
{{out}}
<small>(When fn is 1, or the content of log.txt when it is not.)</small>
Line 339:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(unless (fork)
(out "file.log"
(println *Pid) # First write the daemon's PID to the file
Line 348:
(bye) ) # Child terminates after one hour
 
(bye) # Parent terminates immediately</langsyntaxhighlight>
 
=={{header|Pike}}==
Line 354:
if the first argument is "daemon" the program will be restarted with stdout redirected to "foo".
 
<langsyntaxhighlight Pikelang="pike">int main(int argc, array argv)
{
if (sizeof(argv)>1 && argv[1] == "daemon")
Line 369:
sleep(0.1);
}
}</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
#!/usr/bin/python3
import posix
Line 402:
print("timer running, %s seconds" % str(time.time() - t))
time.sleep(1)
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(require ffi/unsafe)
Line 411:
(with-output-to-file "/tmp/foo"
(λ() (for ([i 10]) (displayln (random 1000)) (flush-output) (sleep 1))))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line># Reference:
# https://github.com/hipek8/p6-UNIX-Daemonize/
 
Line 431:
sleep(1);
spurt $output, DateTime.now.Str~"\n", :append;
}</langsyntaxhighlight>
{{out}}<pre>root@ubuntu:~# su - david
david@ubuntu:~$ ./dumper.p6
Line 484:
=={{header|Sidef}}==
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.
<langsyntaxhighlight lang="ruby">var block = {
for n in (1..100) {
STDOUT.say(n)
Line 501:
 
STDERR.say("Normal mode")
block.run</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
{{works with |Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">#! /usr/bin/env stx --script
(pid := OperatingSystem fork) == 0 ifTrue:[
Stdin close.
Line 520:
Stdout showCR: e'forked new process pid={pid}; now exiting'.
OperatingSystem exit:0
]</langsyntaxhighlight>
{{out}}
<pre>bash-3.2$ ./daemon.st
Line 537:
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}}
<langsyntaxhighlight lang="tcl">package provide daemon 1
package require critcl
 
Line 550:
}
return TCL_OK;
}</langsyntaxhighlight>
These tools can then be used to solve this task:
<langsyntaxhighlight lang="tcl">### Command line argument parsing
if {$argc < 1} {
puts "usage: $argv0 file ?message...?"
Line 571:
proc every {ms body} {eval $body; after $ms [info level 0]}
every 1000 {puts "[clock format [clock seconds]]: $message"}
vwait forever</langsyntaxhighlight>
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.
 
Line 579:
 
The following script is based on the C example though modified a little to run on Ubuntu 20.04.
<langsyntaxhighlight lang="ecmascript">/* dumper.wren */
 
import "./date" for Date
Line 639:
System.print(UT2Date.call(C.time))
C.sleep(1)
}</langsyntaxhighlight>
<br>
We now embed this in the following C program, compile and run it.
<langsyntaxhighlight lang="c">/* gcc dumper.c -o dumper -lwren -lm */
 
#include <fcntl.h>
Line 791:
free(script);
return 0;
}</langsyntaxhighlight>
 
{{out}}
10,333

edits