I'm trying:
#!/bin/bash
if $(ps -C "bm_d21_debug")
then
kill $(ps -C "bm_d21_debug" -o pid=)
echo "exists"
fi
It returns: "PID: command not found"
Not sure what I'm doing wrong?
Consider this line:
if $(ps -C "bm_d21_debug")
You execute the ps command in a command substitution, which returns the command output. The if command then tries to run that output as a command.
The first word of the ps output is PID, which if will handle as the command name. Thus, the "command not found" error.
You just want
if ps -C "bm_d21_debug" >/dev/null; then
echo running
else
echo NOT running
fi
I suggest to use square brackets also:
if [[ $(ps -C "bm_d21_debug") ]]
But this command will always return "yes" ($? = 0)
if ps -C "bm_d21_debug"pkillman page. It'll make your life easier.