Skip to main content
Remove unnecessary "status" check if directory exists already. Use more shell-agnostic `test` command. Escape the PID variable as general programming hygiene.
Source Link
VasiliNovikov
  • 10.5k
  • 5
  • 53
  • 65

If your system implementsOn systems that implement procfs interface such as Linux, you can just check if there is a special file /proc/$PID/status exists:

if [test -ed /proc/$PID"$PID"/status ];; then echo "process exists" fi 

otherwise you can use ps program:

if [ -n "$(ps -p $PID -o pid=)" ] 

In the latter form, -o pid= is an output format to display only the process ID column with no header. The quotes are necessary for non-empty string operator -n to give valid result.

If your system implements procfs interface, you can just check if there is a special file /proc/$PID/status:

if [ -e /proc/$PID/status ]; then echo "process exists" fi 

otherwise you can use ps program:

if [ -n "$(ps -p $PID -o pid=)" ] 

In the latter form, -o pid= is an output format to display only the process ID column with no header. The quotes are necessary for non-empty string operator -n to give valid result.

On systems that implement procfs interface such as Linux, you can just check if /proc/$PID exists:

if test -d /proc/"$PID"/; then echo "process exists" fi 

otherwise you can use ps program:

if [ -n "$(ps -p $PID -o pid=)" ] 

In the latter form, -o pid= is an output format to display only the process ID column with no header. The quotes are necessary for non-empty string operator -n to give valid result.

A less complicated solution for procfs
Source Link
user2683246
  • 3.6k
  • 32
  • 31

If your system implements procfs interface, you can just check if there is a special file /proc/$PID/status:

if [ -e /proc/$PID/status ]; then echo "process exists" fi 

orotherwise you can use ps program:

if [ -n "$(ps -p $PID -o pid=)" ] 

In the latter form, -o pid= is an output format to display only the process ID column with no header. The quotes are necessary for non-empty string operator -n to give valid result.

if [ -e /proc/$PID/status ]; then echo "process exists" fi 

or

if [ -n "$(ps -p $PID -o pid=)" ] 

In the latter form, -o pid= is an output format to display only the process ID column with no header. The quotes are necessary for non-empty string operator -n to give valid result.

If your system implements procfs interface, you can just check if there is a special file /proc/$PID/status:

if [ -e /proc/$PID/status ]; then echo "process exists" fi 

otherwise you can use ps program:

if [ -n "$(ps -p $PID -o pid=)" ] 

In the latter form, -o pid= is an output format to display only the process ID column with no header. The quotes are necessary for non-empty string operator -n to give valid result.

A less complicated solution for procfs
Source Link
user2683246
  • 3.6k
  • 32
  • 31
if [ -n "$PID" -a -e /proc/$PID/status ]; then echo "process exists" fi 

or

if [ -n "$(ps -p $PID -o pid=)" ] 

In the latter form, -o pid= is an output format to display only the process ID column with no header. The quotes are necessary for non-empty string operator -n to give valid result.

if [ -n "$PID" -a -e /proc/$PID ]; then echo "process exists" fi 

or

if [ -n "$(ps -p $PID -o pid=)" ] 

In the latter form, -o pid= is an output format to display only the process ID column with no header. The quotes are necessary for non-empty string operator -n to give valid result.

if [ -e /proc/$PID/status ]; then echo "process exists" fi 

or

if [ -n "$(ps -p $PID -o pid=)" ] 

In the latter form, -o pid= is an output format to display only the process ID column with no header. The quotes are necessary for non-empty string operator -n to give valid result.

added 159 characters in body
Source Link
user2683246
  • 3.6k
  • 32
  • 31
Loading
added 1 character in body
Source Link
jwfearn
  • 29.8k
  • 28
  • 102
  • 125
Loading
Source Link
user2683246
  • 3.6k
  • 32
  • 31
Loading