5

We have some windows box on the network, and shared printers on them.

I've added them through the CUPS web interface, and all of them works like a charm, we can print to them by php:

exec('lp -d vasosamsung file.pdf');

Cool. I worte a Printer class, what makes some validation before the print job ccould start.

The first thing what I check is the printer is exists:

lpstat -p vasosamsung

If it is not says, the destination is invalid, then yes, move on. And here comes the tricky part.

If the answer is idle, or printing, the everything is fine, let's send the job.

BUT!

The problem is it says, the printer is idle, because on my machine the printer has added to the list of printers, but sadly it is offline. This is what I want to catch. More problems come.

Ok, I can deal with it, the jobs are in the queue even if the printer itself is offline (pulled out from the machine).

The real problem is, that when I removed the whole printer thing from my "Printers and Devices" with right click and remove device, CUPS says, it is idle. I've restarted the CUPS by service cups restart, and get a stat:

printer vasosamsung is idle.  enabled since Thu 06 Apr 2017 03:34:26 PM CEST

It is not true. Now on my machine there is now printer like this. The worst thing, if I am send a document to it, it says:

string(40) "request id is vasosamsung-71 (1 file(s))"

and the job is on the web interface:

vasosamsung-71  Unknown     Withheld    1933k   1   held since

Thu Apr 6 15:59:43 2017

My question is: is there a way ti get the phisicaly printer status? Offline, printing etc... not from CUPS?

If it is not possible, can I get somehow, is the printer is in the device list on the target machine at least?

EDIT

Hehe, when I turned off my machine, the printer is idle also :)

I guess, this is because vasosamsung printer is in the CUPS, and that is not the phiscal device, now I see, but my questions are still alives.

vaso123
  • 255

3 Answers3

2

Use lpstat with the -t parameter:

$ lpstat -t
scheduler is running
system default destination: DCP-7065DN
device for DCP-7065DN: usb://Brother/DCP-7065DN?serial=<REDACTED>
DCP-7065DN accepting requests since Tue 19 Nov 2019 04:29:56 PM MST
printer DCP-7065DN is idle.  enabled since Tue 19 Nov 2019 04:29:56 PM MST

The system tells us the printer is accepting requests, eg "Good to go".

0

my solution: in my programs written in C I include the function (or procedure in ALGOL, my first language, in 1964 ...)

#define BUFFERLEN 1024
int isprtready( void )
{ char cmd[BUFFERLEN], buffer[BUFFERLEN], usbstat[]="lsusb > ",
       enabled[]="Hewlett-Packard LaserJet 2420", filename[]="prtreadyXXXXXX";
  int handle, len;

  handle = mkstemp( filename );

  strcpy( cmd, usbstat );
  strcat( cmd, filename );
  system( cmd );
  len = read( handle, buffer, BUFFERLEN );
  buffer[len] = '\0';
  close( handle ); 
  unlink( filename );

  return ( strstr( buffer, enabled ) != (char*)NULL );
}

With the necessary includes
disadvantage: The explicit printer name is in my program. (I have only one computer and one printer, no network and such things.)

karel
  • 122,292
  • 133
  • 301
  • 332
0

So the real question is how to get cups to tell you a printer is not there.

Set printer to deny all and it should not show up as a printer. After the printer is back, deny none.

lpadmin -p printername -u deny:all

lpadmin -p printername -u deny:none

This is what I have had to do locally so my applications don't show me an offline printer.

In you case you are running a server so maybe changing the: -o printer-is-shared=true to false would work as well.

Len
  • 1