I have opened abc.pdf in Document Viewer 3.18.2. I am writing bash script to close the specific document opened in Document Viewer. Any idea ?

  • How did you open the document? Should we assume that other documents are also opened in Document Viewer? And please explain why you want to do this. – sudodus Jan 5 '17 at 15:25
  • I have opened the document manually. It may be more than 1 documents are opened in Document Viewer. Then I running a script to update the document from server. So I want to close the specific document (if open) and then replace it with new document in my directory. – techy Jan 5 '17 at 15:35
  • Do you open it from the command line with for example evince file.pdf or completely via the graphical user interface? – sudodus Jan 5 '17 at 15:52
  • No I have not opened the file with command line. Its opened using GUI by mouse clicking on pdf. And my script should close that document. – techy Jan 5 '17 at 15:55
  • I see. If you open the files from the command line, I know a method to identify it, evince file.pdf & pid=$! will store the process ID in the variable pid, which you can use to kill it with kill $pid in your script. But I don't know, when you started it from the GUI. Maybe you can identify the processes via the command ps -Af|grep evince – sudodus Jan 5 '17 at 16:06

The command

ps -Af|head

displays the a header and the first 9 processes. This helps you identify which column that contains the process ID (PID).

The command

ps -Af|grep evince | grep abc.pdf

displays processes where evince is running files, that contain adc.pdf in the file name. Normally it will be only one such process. And you kill it like you did manually, but with that command in the script.

You can create a variable that contains the PID, and use it for the kill command. In my Lubuntu 16.04.1 LTS system the second column contains the PID. The following two command lines should do the job from a shellscript file.

pid=$(ps -Af|grep evince | grep abc.pdf| tr -s ' ' ' '|cut -d ' ' -f 2)
kill $pid

You may have to modify the first of these lines, so that it picks the correct column or 'field' with the option -f at the end. (And you may have to modify the file name, if it is not always abc.pdf.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.