Linux/Unix command to determine if process is running?

后端 未结 14 994
忘掉有多难
忘掉有多难 2020-11-28 01:54

I need a platform independent (Linux/Unix|OSX) shell/bash command that will determine if a specific process is running. e.g. mysqld, httpd... What

14条回答
  •  没有蜡笔的小新
    2020-11-28 02:36

    Putting the various suggestions together, the cleanest version I was able to come up with (without unreliable grep which triggers parts of words) is:

    kill -0 $(pidof mysql) 2> /dev/null || echo "Mysql ain't runnin' message/actions"
    

    kill -0 doesn't kill the process but checks if it exists and then returns true, if you don't have pidof on your system, store the pid when you launch the process:

    $ mysql &
    $ echo $! > pid_stored
    

    then in the script:

    kill -0 $(cat pid_stored) 2> /dev/null || echo "Mysql ain't runnin' message/actions"
    

提交回复
热议问题