【问题标题】:Best practice to check if a process is running on Linux?检查进程是否在 Linux 上运行的最佳实践?
【发布时间】:2017-04-14 02:22:02
【问题描述】:

我使用下面的脚本安装了厨师服务器。我是 linux 新手,我正在尝试学习如何设置厨师服务器。我运行了 chef.io 提供的命令,脚本成功了。我真的不确定如何检查或我应该做什么来检查进程是否正在运行。关于如何查看进程是否正在运行,Linux 的最佳实践是什么?我可以做些什么来找出我需要知道的内容?

        #!/bin/bash \
echo "Do your provisioning here" \
sudo wget https://packages.chef.io/files/stable/chef-server/12.14.0/el/7/chef-server-core-12.14.0-1.el7.x86_64.rpm \
sudo chmod a+x chef-server-core-12.14.0-1.el7.x86_64.rpm
sudo rpm -Uvh ./chef-server-core-12.14.0-1.el7.x86_64.rpm
sudo chef-server-ctl reconfigure \
sudo openssl rsa -in private.pem -outform PEM -pubout -out ~/.ssh/chef-server.pem \
sudo chef-server-ctl user-create admin 'admin' 'email' 'password' --filename ~/.ssh/chef-server.pem \
sudo openssl rsa -in private.pem -outform PEM -pubout -out ~/.ssh/chef-server-validator.pem \
sudo chef-server-ctl org-create short_name 'idevops' --association_user admin --filename ~/.ssh/chef-server-validator.pem \
sudo openssl rsa -in private.pem -outform PEM -pubout -out ~/.ssh/chef-coffee-server-validator.pem \
sudo chef-server-ctl org-create 4thcoffee 'iDevops 4th Coffee' --association_user admin --filename ~/.ssh/chef-coffee-server-validator.pem \
sudo chef-server-ctl install chef-manage \
sudo chef-server-ctl reconfigure \
sudo chef-manage-ctl reconfigure \
sudo chef-server-ctl install opscode-push-jobs-server \
sudo chef-server-ctl reconfigure \
sudo opscode-push-jobs-server-ctl reconfigure \
sudo chef-server-ctl install opscode-reporting \
sudo chef-server-ctl reconfigure \
sudo opscode-reporting-ctl reconfigure \
sudo chef-server-ctl install PACKAGE_NAME --path /path/to/package/directory \
sudo chef-server-ctl install chef-manage --path /root/packages \
sudo mkdir /etc/opscode && sudo touch /etc/opscode/chef-server.rb \
sudo echo "license['nodes'] = 0" >> /etc/opscode/chef-server.rb \
sudo chef-server-ctl reconfigure

【问题讨论】:

  • 我对 chef 不熟悉,但是对于大多数服务器,您应该能够通过某种请求来查询它们。
  • 可能停止在 bash 脚本中执行此操作,而改用 chef-ingredients 食谱。

标签: linux bash process chef-infra


【解决方案1】:

好的,有几个地方你可以在网上找到它们,你要做的第一件事就是检查进程是否正在运行。

ps 辅助 | grep 进程名

然后,如果它正在运行并且您仍然无法访问它。您将需要使用 netstat 命令和 grep 端口。

net stat -anp | grep portnumber

您查看服务是否在其应有的端口上运行。它会说它在听。侦听意味着端口正在该端口上寻找通信。这意味着应用程序已启动并正在端口上寻找通信,或者该端口正在被另一个应用程序使用,这就是它没有启动的原因。

通常您会查看 lgos tail -f -n 100 /path/to/log/file
-n 是行数 -f 是一个连续的跟随,所以它是你观看文件的方式。如果你不指定它,它只会在屏幕上显示 100 行。

【讨论】:

    【解决方案2】:

    如果你只是想手动检查,登录服务器运行:

    ps auxw | grep YOUR_PROCESS_NAME
    

    【讨论】:

      【解决方案3】:

      首先,我不会从脚本运行命令,直到我知道它们可以工作。 您可能没有运行该服务:

          sudo service chef-server status #would be a good place to start 
      

      要检查一个进程是否在 linux 上运行,我建议从以下开始:

          ps aux | grep <part of the process name>
          ps aux | grep chef
      

      如果你不喜欢 aux 的输出,你也可以使用 -ef like:

          ps -ef | grep <part of the process name>
          ps -ef | grep chef
      

      如果你知道进程应该在哪个端口上打开,你可以使用 netstat:

          netstat -anp | grep <port number>
      

      示例:寻找正在运行的 nginx 或 apache 服务器:

          netstat -anp | grep 80
      

      看日志我喜欢用tail命令:

          tail -f /var/log/<name of application folder/<name of log>
      

      示例:查看 nginx 日志:

          tail -f /var/log/nginx/nginx.log
      

      有时查看系统日志也很有帮助:

          tail -f -n 100 /var/log/syslog
      

      如果寻找来自其他机器的传入连接:

          sudo tcpdump -vvv -i any port <port number>
      

      【讨论】:

        【解决方案4】:

        您发布的输出中有一个相对较大的提示(例如chef-server-ctl status),

        The status subcommand is used to show the status of all services available 
        to the Chef server. The results will vary based on the configuration of a 
        given server.
        

        或者,chef-server-ctl status SERVICE_NAME

        where SERVICE_NAME represents the name of any service that is listed after 
        running the service-list subcommand.
        

        另请参阅chef-server-ctl (executable)start 子命令(靠近页面底部)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-05-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-28
          • 2013-07-21
          • 2011-04-17
          相关资源
          最近更新 更多