I am looking for a single line that does return the pid of a running process.
Currently I have:
ps -A -o pid,cmd|grep xxx|head -n 1
And this returns the fist pid, command
. I need only the first number from the output and ignore the rest. I suppose sed
or awk
would help here but my experience with them is limited.
Also, this has another problem, it will return the pid of grep if the xxx is not running.
It's really important to have a single line, as I want to reuse the output for doing something else, like killing that process.
Best Answer
If you just want the pid of the process you can make use of
pgrep
if available.pgrep <command>
will return the pid of the command (or list of pids in case there are more than one instance of the command running, in which case you can make use ofhead
or other appropriate commands)Hope this helps!