djflu
October 19, 2018, 7:18am
1
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:
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)
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"):
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.
???
RudiC
October 19, 2018, 8:35am
2
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
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".
djflu
October 19, 2018, 10:53am
3
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
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).
RudiC
October 19, 2018, 11:08am
4
You might want to go for the ( man ps
)
etimes ELAPSED elapsed time since the process was started, in seconds.
format specifier (in seconds for ease of evaluation).
djflu
October 23, 2018, 4:39am
5
rudic:
You might want to go for the ( man ps
)
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:
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
RudiC
October 23, 2018, 5:34am
6
You cited my post, but did you read resp. understand it?
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
1 Like