Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
up vote 2 down vote accepted

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).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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