1

I need advice:

How to verify which is the last scan_sys.sh process that run in my linux or solaris ?

my target is to kill all scan_sys.sh process , except the latest scan_sys.sh ( the last execute /usr/scan_sys.sh script )

ps -ef | grep scan_sys.sh

.

root   367     1   0   Nov 08 ?           0:21 /usr/scan_sys.sh
root 27517 27515   0 17:27:18 ?           0:01 /usr/scan_sys.sh
root 18939   367   0   Nov 15 ?           0:00 /usr/scan_sys.sh
root 18940 18939   0   Nov 15 ?           0:01 /usr/scan_sys.sh
root 27515   367   0 17:27:18 ?           0:00 /usr/scan_sys.sh
2

Use ps option -o to select fields you want to display to show process pid, start time and command name, optionally selecting the processes you're interested in right away (-C), sort on start time, kill all but the last one.

Since ps is notoriously known to be a command line options hell, you'll have to check the man page for your implementation. For me the equivalent of the following works on linux (to list the appropriate PIDs):

ps -C scan_sys.sh -o pid --sort start | sed '$d'

Remember to quote the $ in sed script with single quotes (at least in bash) as otherwise it will be expanded to environment variable d. You can send the PIDs to kill e.g. via xargs or by command substitution (`` or $() in bash).

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.