Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tcl Daemons

40 views
Skip to first unread message

John C. Westmoreland

unread,
Jan 31, 2001, 9:33:52 PM1/31/01
to
Hello,

Can someone point me to an example of how to build a daemon
with Tcl? I've looked at the Event-Driven programming section
in the Welch book - but if someone could point me to 'real-world'
example - I'd appreciate it.

Thanks In Advance,
John

Darren New

unread,
Jan 31, 2001, 11:40:48 PM1/31/01
to
Here's a very simple program to poke holes in a firewall.
If you make a program that reads stdin and writes stdout, you can run it
under inetd, too.

--
Darren New / Senior MTS & Free Radical / Invisible Worlds Inc.
San Diego, CA, USA (PST). Cryptokeys on demand.
Ignorance can be cured. Naivety cures itself.

proxy.tcl

Tom Poindexter

unread,
Feb 1, 2001, 1:00:44 AM2/1/01
to
In article <kW3e6.1774$DL2.2...@paloalto-snr1.gtei.net>,


You probably want to use either the Expect or TclX (Extended Tcl) extensions
to access the Posix system calls that are needed to do the things needed to
disassociate from the tty, set umask, become a new process group leader, etc.

Don Libes' fine book "Exploring Expect" has a chapter on 'Background
Processing' that covers background and tty disassociation.

Here's a bit of TclX code that I have used in the past, it's a Tcl
translation of the late W. Richard Stevens' code from his book "Unix Network
Programming" (also see Stevens' "Advanced Programming in the Unix
Environment").

package require Tclx
proc shutdown {} {
# whatever cleanup you need to do
exit
}

proc dispatcher {} {
# do your work here! this sample just opens a log file and writes to it.
while 1 {
set fd [open /tmp/daemon2.log w]
puts $fd "daemon2.tcl last ran at [clock format [clock seconds]]"
close $fd
after 5000
}
}
proc daemonize {} {
close stdin
close stdout
close stderr
if {[fork]} {exit 0}
id process group set
if {[fork]} {exit 0}
set fd [open /dev/null r]
set fd [open /dev/null w]
set fd [open /dev/null w]
cd /
umask 022
return [id process]
}
daemonize
signal ignore SIGHUP
signal unblock {QUIT TERM}
signal trap {QUIT TERM} shutdown
dispatcher

--
Tom Poindexter
tpoi...@nyx.net
http://www.nyx.net/~tpoindex/

Adrian Davis

unread,
Feb 1, 2001, 4:59:48 AM2/1/01
to
How about "nohup"?

Regards,
=Adrian=

John C. Westmoreland <john.wes...@c-cube.com> wrote in message
news:kW3e6.1774$DL2.2...@paloalto-snr1.gtei.net...

Chang LI

unread,
Feb 1, 2001, 9:54:01 AM2/1/01
to
Are these fork, signal available on NT?

Chang LI
Neatware

Tom Poindexter wrote in message <98101073...@irys.nyx.net>...

Glenn W Jackman

unread,
Feb 1, 2001, 11:19:13 AM2/1/01
to
Tom Poindexter wrote:
> proc daemonize {} {
> close stdin
> close stdout
> close stderr
...

> set fd [open /dev/null r]
> set fd [open /dev/null w]
> set fd [open /dev/null w]

What do these 3 magic lines do? Do they somehow re-open stdin,
stdout and stderr?

--
Glenn

Frank Pilhofer

unread,
Feb 1, 2001, 1:13:07 PM2/1/01
to

Yes. Newly opened files receive the lowest available filedescriptors,
and these are 0, 1, and 2 after closing them.

Years ago, I read a web page about "Unix Horror Stories". One of them
was about a database application that was daemonized. The developer
closed stdin, stdout and stderr, but didn't re-open them. Therefore,
one file opened by the db frontend happened to be fd 1, stdout. The
database was then garbled beyond repair from all the printf() debug
code left in the program.

That's a story I always tell in my courses when the question "is it
really necessary" comes up (and it always does).

Frank


--
Frank Pilhofer ........................................... f...@fpx.de
Most people don't act stupid, it's the real thing! - A. E. Neuman

Tom Poindexter

unread,
Feb 1, 2001, 3:49:29 PM2/1/01
to
In article <ZKee6.5950$nb.8...@newscontent-01.sprint.ca>,

Chang LI <cha...@neatware.com> wrote:
>Are these fork, signal available on NT?
>

Check the TclX extension - I'm not a regular NT user. TclX is ported to NT,
I don't know how much of the Posix commands are ported.

Also, check into the various Posix on NT solutions, Cygwin, UWIN, etc.

John C. Westmoreland

unread,
Feb 2, 2001, 2:40:57 AM2/2/01
to
Thanks for the suggestions - does anyone have an example for
Tcl8.3.2 running under NT?

Thanks,
John


In article <kW3e6.1774$DL2.2...@paloalto-snr1.gtei.net>,
john.wes...@c-cube.com says...

Donal K. Fellows

unread,
Feb 2, 2001, 6:31:50 AM2/2/01
to
"John C. Westmoreland" wrote:
> Thanks for the suggestions - does anyone have an example for
> Tcl8.3.2 running under NT?

So you're looking to run Tcl as a service under the SCM? That's rather
different (and my documentation on it is not going to help; I know nothing
about it myself, being a Unix programmer... :^)

Donal.
--
Donal K. Fellows http://www.cs.man.ac.uk/~fellowsd/ fell...@cs.man.ac.uk
-- [Perl] is mostly pretty "intuitive" in a certain way.
-- Jason Williams <ja...@compsoc.man.ac.uk>

Tom Poindexter

unread,
Feb 2, 2001, 10:32:52 AM2/2/01
to
In article <dwte6.5123$Yl6.4...@paloalto-snr1.gtei.net>,

John C. Westmoreland <john.wes...@c-cube.com> wrote:
>Thanks for the suggestions - does anyone have an example for
>Tcl8.3.2 running under NT?

Matt Newman has 'tclsvc', which may be what you want.
http://www.sensus.org/tcl/index.htm

Darren New

unread,
Feb 2, 2001, 1:12:45 PM2/2/01
to
Tom Poindexter wrote:
> Matt Newman has 'tclsvc', which may be what you want.
> http://www.sensus.org/tcl/index.htm

I have used this and it works. One thing to be aware of is that you need to
wake up every second or so, or the services control panel will think your
app is crashed. E.g., you'll try to stop it and the control panel will tell
you it can't be stopped.

I just put

proc again {} {
after 1000 again
} ; again

at the bottom of my main routine (which was otherwise event driven anyway)
to make sure code woke up regularly, and that seemed to fix the problem.

Otherwise, it's a fairly nice package. Not 100% smooth in terms of
installation and such, but entirely sufficient for in-house use.

tony summerfelt

unread,
Feb 3, 2001, 4:25:45 PM2/3/01
to
On Fri, 02 Feb 2001 15:32:52 GMT, Tom Poindexter wrote:

>Thanks for the suggestions - does anyone have an example for
> >Tcl8.3.2 running under NT?
>
> Matt Newman has 'tclsvc', which may be what you want.
> http://www.sensus.org/tcl/index.htm

there's also a generic nt/2k program called 'srunner' which will allow any
*.exe to run as a service. if you freewrap your program, srunner can run it
as a service...but if you have more than once tcl program that tclsvc is
probably a better choice...

--
*----------------------------------------
|
|http://www.biosys.net/snowzone
|
|remove myspleen to email
|
*-----------------------

0 new messages