How to kill printer PIDs except the spooler PID?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to kill printer PIDs except the spooler PID?
# 1  
Old 10-19-2018
How to kill printer PIDs except the spooler PID?

First of all, I'd like to congratulate this big family and all members for all the work you do!
I'm trying to do an script or sentence which kills an specific printers PIDs: all printers PIDs older than 72h running in the server.

Steps:
1.- List all printers PID sorting by date:
Code:
ps -eaf | grep -e "UID" -e "lpsched" | grep -v grep | sort -rk6
   UID   PID  PPID  C    STIME TTY       TIME COMMAND
    lp 22948 11814  0 12:36:58 ?         0:00 /usr/sbin/lpsched
    lp 19332 11814  0 08:45:17 ?         0:00 /usr/sbin/lpsched
    lp 29793 11814  0  Sep 30  ?         0:00 /usr/sbin/lpsched
    lp 11814     1 12  Aug 28  ?        238:08 /usr/sbin/lpsched
    lp 18598 11814  0  Oct 17  ?         0:00 /usr/sbin/lpsched
    lp 26198 11814  0  Oct 16  ?         0:00 /usr/sbin/lpsched
    lp 24981 11814  0  Oct 16  ?         0:00 /usr/sbin/lpsched
    lp 13996 11814  0  Oct 13  ?         0:00 /usr/sbin/lpsched
    lp  5802 11814  0  Oct 12  ?         0:00 /usr/sbin/lpsched
    lp 24510 11814  0  Oct 10  ?         0:00 /usr/sbin/lpsched
    lp  4585 11814  0  Oct  8  ?         0:00 /usr/sbin/lpsched
    lp 26585 11814  0  Oct  7  ?         0:00 /usr/sbin/lpsched
    lp 25739 11814  0  Oct  2  ?         0:00 /usr/sbin/lpsched

2.-Isolate PPID = spooler ID (no killed)
Code:
ps -eaf | grep -e lpsched | grep -v grep | awk '{if ($3>1) print $3}' | tail -n 1
   11814

3.-PIDs to be killed (no kills the spooler PPID "11814"):
Code:
ps -eaf | grep -e lpsched | grep -v grep | awk '{print $2}' | pgrep -e 72:00:00 lpsched
  24510
  4585
  29793
  11814
  26585
  13996
  25739
  11319
  5802

4.- Kill all the PIDs of step 3 except the spooler PPID 11814.
Code:
???

# 2  
Old 10-19-2018
Welcome to the forum.


I'm not sure I can follow your specification, as it sometimes seems to

- contradict your sample code / output (e.g. "no kills the spooler PPID "11814"" - are you talking of the process ID or the parent pr. ID?)
- refer to data NOT in the sample: 72:00:00
- use non-existent options (-e to pgrep; not found for pgrep on linux nor FreeBSD, only for pkill)


SHOULD you want to kill the processes that are childs to the lp root / parent process identified by PPID = 1, try

Code:
ps -eaf | awk 'BEGIN {printf "kill "} /lpsched$/ && $3 > 1 {printf "%s ", $2} END {printf ORS}' 
kill 22948 19332 29793 18598 26198 24981 13996 5802 24510 4585 26585 25739

If happy with the result, pipe the output through a shell (append | sh), or source it using "process substitution".
# 3  
Old 10-19-2018
Quote:
Originally Posted by RudiC
Welcome to the forum.


I'm not sure I can follow your specification, as it sometimes seems to

- contradict your sample code / output (e.g. "no kills the spooler PPID "11814"" - are you talking of the process ID or the parent pr. ID?)
- refer to data NOT in the sample: 72:00:00
- use non-existent options (-e to pgrep; not found for pgrep on linux nor FreeBSD, only for pkill)


SHOULD you want to kill the processes that are childs to the lp root / parent process identified by PPID = 1, try

Code:
ps -eaf | awk 'BEGIN {printf "kill "} /lpsched$/ && $3 > 1 {printf "%s ", $2} END {printf ORS}' 
kill 22948 19332 29793 18598 26198 24981 13996 5802 24510 4585 26585 25739

If happy with the result, pipe the output through a shell (append | sh), or source it using "process substitution".
Yes, I want to kill all lpsched process older than 72h and excepting the spooler id (column 3 -11814).

I can try this option and I will let you know, but it doesn't filter by date (older than 72h).
# 4  
Old 10-19-2018
You might want to go for the (man ps)
Code:
etimes      ELAPSED   elapsed time since the process was started, in seconds.

format specifier (in seconds for ease of evaluation).
# 5  
Old 10-23-2018
Quote:
Originally Posted by RudiC
You might want to go for the (man ps)
Code:
etimes      ELAPSED   elapsed time since the process was started, in seconds.

format specifier (in seconds for ease of evaluation).

Thanks a lot for the info, but I don't know how to isolate PIDs older than 72h:
Code:
ps -aef |grep -i lpsched | grep -v grep | sort -rk6 
       lp 18188 11814  0 08:11:54 ?         0:00 /usr/sbin/lpsched
      lp 12650 11814  0 08:56:28 ?         0:00 /usr/sbin/lpsched
      lp  9131 11814  0 12:19:57 ?         0:00 /usr/sbin/lpsched
      lp  1078 11814  0 09:08:07 ?         0:00 /usr/sbin/lpsched
      lp 29793 11814  0  Sep 30  ?         0:00 /usr/sbin/lpsched
      lp 11814     1 23  Aug 28  ?        245:18 /usr/sbin/lpsched
      lp 19332 11814  0  Oct 19  ?         0:00 /usr/sbin/lpsched
      lp 18598 11814  0  Oct 17  ?         0:00 /usr/sbin/lpsched
      lp 26198 11814  0  Oct 16  ?         0:00 /usr/sbin/lpsched
      lp  5802 11814  0  Oct 12  ?         0:00 /usr/sbin/lpsched
      lp 24510 11814  0  Oct 10  ?         0:00 /usr/sbin/lpsched
      lp  4585 11814  0  Oct  8  ?         0:00 /usr/sbin/lpsched
      lp 26585 11814  0  Oct  7  ?         0:00 /usr/sbin/lpsched
      lp 25739 11814  0  Oct  2  ?         0:00 /usr/sbin/lpsched

 ps -eaf | grep -e lpsched | grep -v grep | awk '{if (/lpsched$/ && $3 > 1) printf "%s ", $2}'
24510 19332 12650 4585 29793 26585 26198 25739 9131 18598 5802 18188 

# 6  
Old 10-23-2018
You cited my post, but did you read resp. understand it?
# 7  
Old 10-23-2018
Quote:
Originally Posted by djflu
Thanks a lot for the info, but I don't know how to isolate PIDs older than 72h
What RudiC was suggesting (and, if you would have undergone the effort to actually read the man page, as suggested, you'd have found that out yourself) was that ps has a -o-option which you can use to tailor its output to exactly what you need. You might want to consider using this instead of the -eaf you use right now.

Using this device you can use the keyword RudiC gave you to get the number of seconds since the process was started. "3 days" are then a matter of multiplying 86400 (the number of seconds in a day, 24x60x60) by 3 - an exercise left to the interested reader - and testing against this threshhold value.

I hope this helps.

bakunin

Last edited by bakunin; 10-23-2018 at 07:33 PM..
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Kill pid

I created a program to kill long running pid processes. I am getting the following error message: -f command cannot be found. I also want to count the number of pids that are killed and append the results to a text file. I am new to shell script programming. 1.The first part of code... (10 Replies)
Discussion started by: dellanicholson
10 Replies

2. Proxy Server

Samba kill the locked files from a useraccount by multiple smbd pids

Details Samba server: Release: 5.10 Kernel architecture: sun4u Application architecture: sparc Hardware provider: Sun_Microsystems Kernel version: SunOS 5.10 Generic_142909-17 Samba version: Samba version 3.5.6 Smb.conf file section Global: # smb.conf for Airbus Industries fuer... (0 Replies)
Discussion started by: Jean-Guillaume
0 Replies

3. Shell Programming and Scripting

Kill PID with one liner

Hello Friends, I've been trying to write a one line which checks java processes and filter them for a user (testuser) and then check process arguments with PARGS command and then check if there is certain patterns exists in pargs output then kill the process. I have tried the following so... (2 Replies)
Discussion started by: EAGL€
2 Replies

4. Shell Programming and Scripting

Need help to kill pids

Hi there !!! I am writing a script to kill the pids on different linux boxes :cool: the output of my command gives the pids running on that box, but how can I kill all the pids without looping? :confused: Code: ssh $i ps -fu $USER | grep ManServer | grep -v grep | awk '{print $2}' | kill ... (4 Replies)
Discussion started by: prany_cool
4 Replies

5. Shell Programming and Scripting

Kill a PID using script

Hi, I wrote a script to kill a process id. I am able to kill the PID only if I enter the root password in the middle of the execution because I did not run as root i.e after i run the script from the terminal, instead of killing directly, it is killing only after entering the pass when it... (12 Replies)
Discussion started by: rajkumarme_1
12 Replies

6. Shell Programming and Scripting

Track and kill the PIDS

I have a script that conducts some SSH calls and I would like to capture the child info so that I can do a sleep and then a cleanup to make sure they do not stay out there as ghosts. I was told I could do something like this... #!/bin/sh for m = job1, job2, job3 x=1... (4 Replies)
Discussion started by: LRoberts
4 Replies

7. Shell Programming and Scripting

When kill [pid] does not work...

Hi, On my Linux machine, using Bash, I sometimes run into a situation where doing the following does not seem to work at all. kermit@fastbox ~ $ ps -A | grep firefox-bin 5375 ? 00:06:57 firefox-bin <defunct> 5624 ? 00:00:00 firefox-bin kermit@fastbox ~ $ kill 5624... (7 Replies)
Discussion started by: kermit
7 Replies

8. UNIX for Advanced & Expert Users

KILL without PID

Hellow Experts i have one problem. i run one script in backgroun. and i want to kill that script with only script name..... so what's the solution.. for your info my script name is "testscript" n it contains "sleep 100" thanks.... (16 Replies)
Discussion started by: luckypower
16 Replies

9. Shell Programming and Scripting

KILL PID, intern should kill another PID.

Hi All, In my project i have two process runs in the back end. Once i start my project, and execute the command ps, i get below output: PID TTY TIME CMD 9086 pts/1 0:00 ksh 9241 pts/1 0:02 java 9240 pts/1 0:00 shell_script_bg java with 9241 PID is the main... (4 Replies)
Discussion started by: rkrgarlapati
4 Replies

10. Programming

printer status for print spooler: help

Hey people I am currently working on a print spooler for unix over a network. I need help regarding the status of the printer. Is there any way to know when the printer has finished a previous job, so that the next job from the queue can be processed. Also is there any other way to print other... (0 Replies)
Discussion started by: rage
0 Replies
Login or Register to Ask a Question