0

This is the list of my processes:

$ ps
PID   USER     TIME  COMMAND
    1 root      0:00 bash -c cp -R /docker /var/lib; echo Loading...; wrapdocker
    7 root      0:00 bash --login
  312 root      0:00 dockerd
  321 root      0:00 containerd --config /var/run/docker/containerd/containerd.toml --log-level info
  488 root      0:00 ps

I want to check if the process dockerd is running.
When I run: ps | grep dockerd it prints two processes:

$ ps | grep 'dockerd'
  312 root      0:00 dockerd
  492 root      0:00 grep dockerd  

I need to find it by the specific command line which is dockerd and not grep dockerd.

I need it as part of a shell script to check if dockerd is running.

4

You should use pgrep process instead of ps aux | grep process.

Like, currently gnome-terminal is running, so

ps aux | grep gnome-terminal
user    2045  0.9  0.9 459456 36084 ?        Ssl  17:33   0:00 
/usr/lib/gnome-terminal/gnome-terminal-server
user    2206  0.0  0.0  12780   996 pts/0    S+   17:33   0:00 grep 
gnome-terminal

Where as

pgrep gnome-terminal
2045

And currently chome is not running, so

ps aux | grep chrome
user    2212  0.0  0.0  12780   976 pts/0    S+   17:34   0:00 grep chrome

But pgrep chrome will return nothing.

In your case, pgrep dockerd will work.

  • 2
    pgrep matches on the process name unless you pass -f while ps aux shows the process arg lists. pgrep does an extended regexp match so pgrep -f gnome-terminal would still match on a process that runs grep gnome-terminal. Here you'd need pgrep -fx /usr/lib/gnome-terminal/gnome-terminal-server to only match on processes whose full arg list is /usr/lib/gnome-terminal/gnome-terminal-server – Stéphane Chazelas Apr 1 at 12:18
0
#!/bin/bash

ps -ef | grep -q "[d]ockerd"
if [ "$?" -eq "0" ]
then
      echo "dockerd is running"
else
      echo "dockerd is not running"
fi
  • 1
    That still matches on any process that has dockerd in its arg list or is running as the dockerd user – Stéphane Chazelas Apr 1 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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