Skip to main content

Timeline for answer to How to check if a process id (PID) exists by FDS

Current License: CC BY-SA 4.0

Post Revisions

16 events
when toggle format what by license comment
May 6, 2024 at 16:08 comment added Michael C. Chen It seems that if a process is run by something/someone else and I don't have the authority to see/kill it, then returning non-zero (same result as if the process is not running) is actually desired. Both I am not allowed to kill, or if the pid had been recycled and being used by another user so is no longer the process I am looking for, I would prefer error. This is also the more secure model. If we truly want to access across all processes, we would use sudo, root user, or the user/app that spawned the pid. Though kill -0 doesn't protect against pid recycling either.
Nov 9, 2023 at 16:47 comment added Tomsim the ps command sometime hang on my VM system so checking /proc/$pid/ is a safe way to check if the process still exist
Jan 13, 2022 at 3:10 comment added Richard Tyler Miles ps -p seems to be the correct solution for RHEL
S Oct 15, 2021 at 0:32 history edited Shane Bishop CC BY-SA 4.0
Added inline code tags instead of quotation, incorporated comment and improved syntax highlighting
S Oct 15, 2021 at 0:32 history suggested Cadoiz CC BY-SA 4.0
Added inline code tags instead of quotation, incorporated comment and improved syntax highlighting
Oct 14, 2021 at 13:58 comment added Cadoiz Just for other noobs like I seem to be: > /dev/null does not read > 0(comparison), but forwards the output to nothingness. I just proposed an update to your brilliant and +1ed answer and incorporated the comment by AnrDaemon. Alternatively to ps -p $PID > /dev/null, you can internally check (without a new process) whether a PID is running with [[ ! -z ${PID+x} ]], as statet e.g. here.
Oct 14, 2021 at 13:08 review Suggested edits
S Oct 15, 2021 at 0:32
Sep 25, 2020 at 5:06 comment added Mikko Rantalainen Actually, test is often shell builtin and only executes syscalls by the shell itself.
Sep 22, 2020 at 12:42 comment added Mikko Rantalainen If you want to know if the process with id $PID exists, you can just do test -d /proc/$PID instead of starting additional processes. Note that you cannot ever know if a process exists in some another PID namespace.
Aug 22, 2019 at 21:58 comment added antichris This does not work on BusyBox. OpenWrt only recognizes w for "wide output", Alpine takes only -o for filtering output columns. Docs also mention a -T for showing threads. There is no -p parameter.
Sep 8, 2017 at 23:18 comment added David Roundy Portability is a good reason to avoid using /proc, but linux breaking its ABI is not a scenario I would particularly worry about.
Apr 16, 2017 at 10:56 comment added user137369 I can confirm that on macOS Sierra, this works. Also, the -p is unnecessary, at least in that case. ps $PID has the exact same result.
Feb 24, 2016 at 1:46 comment added Douglas Correa ps -p always returns status code 0 in os x because it prints an empty list of process when it not matches any running process
Feb 23, 2016 at 17:43 comment added akahunahi ps -p #### worked fine for me under Ubuntu 14.04, +1 thanks!
Feb 23, 2014 at 14:51 comment added IttayD ps -p always returns status of 0 for me
Apr 2, 2013 at 21:29 history answered FDS CC BY-SA 3.0