From: john@sekrit.WPI.EDU (John Stoffel)
Newsgroups: comp.lang.perl
Subject: Re: Perl Printer Daemon?
Date: 12 Nov 92 10:14:37
Organization: Worcester Polytechnic Institute
Distribution: comp
NNTP-Posting-Host: sekrit.wpi.edu
In-reply-to: glewis@fws204.intel.com's message of 11 Nov 92 20:19:20 GMT

>>>>> On 11 Nov 92 20:19:20 GMT, glewis@fws204.intel.com (Glenn M. Lewis - ICD ~) said:


Glenn> 	I have a peculiar application, for which I think Perl would be
Glenn> a good candidate to get the job done, but need input and advice.

Glenn> 	I would like to start up a Perl script on my UNIX workstation
Glenn> that simply looks in a directory for any file.  If no file is there,
Glenn> sleep for a little while and then check again.  If there *is* a file,
Glenn> I would like the script to perform an eensy bit of filtering on it
Glenn> (strip out ^M and ^D), and then send it off to a UNIX printer, using
Glenn> "lpr" and the "-r" flag to remove the file.  (I have very little
Glenn> understanding of "cron" or if "cron" could be used by an average-joe
Glenn> [read: non-super] user to make sure this script is always running, but
Glenn> that is a side issue that maybe someone could teach me about.)

This program is pretty trivial, but knowing me, it would take for ever
to debug.  Here is my stab at it, off the top of my head...

#!/usr/local/bin/perl

$file = "/usr1/john/foo";

while(1) {
	if (-e $file) {
		open(IFILE,"<$file") || die "Can't read $file: $!\n";
		open(OFILE,">$file.tmp") || die "Can't create
$file.tmp: $!\n";
		while (<IFILE>) {
			s/\cD|\cM//g;
			print OFILE;
			}
		close(IFILE);
		close(OFILE);
		unlink($file);
	}
	sleep(60);
}

Glenn> 	I could probably figure out how to do the above part, with a
Glenn> little time.  Now comes the tricky part...  I would like this script
Glenn> to also check the printer queue every so often, to see if it is making
Glenn> progress.  Yes, this sketchy, because huge print jobs will take a long
Glenn> time, and from the "lpq" standpoint, it is not easy to determine if
Glenn> progress is being made.  Neglecting this side issue, if the script
Glenn> determines that progress is *not* being made, I would like it to:

Glenn>     1) Log into a remote machine with an embedded username and
Glenn>        password (which should be secure, btw),
Glenn>     2) Execute commands on the remote machine to get the printer going
Glenn>        again, and
Glenn>     3) logout of the remote machine and continue checking the
Glenn>        directory.

I think your best bet would be to have TWO seperate programs to do all
of the above.  The first program would wait for the file and then
print it.  The second one would watch the printer queue, and do the
appropriate stuff to restart it if needed.  This would close the LARGE
security hole of having to embed a username and password into the
first script.

This script will be a little hairier than the first, and pretty
dependent on whether or not you are using BSD nad SYSV unix.  Let me
know if you would like some more help.

					John


--
Youth of today!  Join me in a mass rally for traditional mental attitudes!
-------------------------------------------------------------------------------
john@wpi.wpi.edu | Work Station Specialist | Worcester Polytechnic Institute
John Stoffel     | 508-831-5512 (work)     | Worcester, MA  01609
Newsgroups: comp.lang.perl