Simanas
Simanas

Reputation: 2793

How to check if pdf printing is finished on linux command line

I have a bunch of files that I need to print via PDF printer and after it is printed I need to perform additional tasks, but only when it is finally completed.

So to do this from my python script i call command "lpr path/to/file.doc -P PDF"

But this command immediately returns 0 and I have no way to track when printing process is finished, was it successful or not etc...

There is an option to send email when printing is done, but to wait for email after I start printing looks very hacky to me.

Do you have some ideas how to get this done?

Edit 1 There are a plenty of ways to check if printer is printing something at current moment. Therefore at the moment after I start printing something I run lpq command every 0.5 second to find out if it is still printing. But this looks to m e not the best way to do it. I want to be able get alerted or something when actual printing process is finished. Was it successful or not etc...

Upvotes: 4

Views: 3058

Answers (2)

Fred Foo
Fred Foo

Reputation: 363727

If you have CUPS, you can use the System V-compatible lp instead of lpr. This prints, on stdout, a job id, e.g.

request id is PDF-5 (1 file(s))

(this is for the virtual printer cups-pdf). You can then grep for this id in the output of lpstat:

lpstat | grep '^PDF-5 '

If that produces no output, then your job is done. lpstat -l produces more status information, but its output will also be a bit harder to parse.

Obviously, there are cleaner Python solutions then running this actual shell code. Unfortunately, I couldn't find a way to check the status of a single job without plowing through the list of jobs.

Upvotes: 2

user1006989
user1006989

Reputation:

You can check the state of the printer using the lpstat command (man lpstat). To wait for a process to finish, get the PID of the process and pass it wait command as argument

Upvotes: 1

Related Questions