shell脚本来检查,如果进程正在运行 [英] Shell script to check if process is running

查看:398
本文介绍了shell脚本来检查,如果进程正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些怪异的行为怎么回事,导致我认为可能有一些事情...

There is some weird behavior going on that leads me to think there may be something going on...

所以,我必须由cron执行的shell脚本。基本上是指,以检查是否Node.js的正在运行。如果是这样,请登录日期......如果不是那么没有登录!在我建造它的时候我测试它,看到它记录当一个节点脚本运行,当它停止运行没有登录...

So I have a shell script executed by cron. Basically it is meant to check if Node.js is running. If so, log the date... if not then log nothing! At the time I built it I tested it and saw it logged when a node script was running and did not log when it stopped running...

最近我知道的节点去了,并认为这是检查的绝佳机会,如果脚本做了它应该做的事。它的剪掉!它的 ...:(

Recently I knew Node went down and thought it was the perfect opportunity to check if the script did what its supposed to do. It didnt! And it does not... :(

下面是脚本:

#!/bin/bash

if ps -Al | grep -v grep | grep -q node 
then
        date > /etc/nodeCheck.log 
else
        date > /dev/null 
fi

在这个.SH权限是否正确,使用存在路径,运行

Permissions on this .sh are correct, paths used exist, running

$ps -A | grep -v grep | grep -q node

没有返回值以及

$echo $?
1

所以应该不会是去else块?节点启动后启动的进程。 shell脚本不能正常工作既当我通过SSH连接的时候由cron或者我跑。

So shouldn't it be going to the else block? node is a process started after bootup. The shell script does not work correctly both when run by cron or by me when I am SSH'd in.

我失去了一些东西在这里根本?

Am I missing something fundamental here?

TIA

尼科

推荐答案

除非你正在运行的 BSD PS ,你应该能够使用 -C 标记或指派,

Unless you're running BSD ps, you should be able to use a -C flag or pgrep

-C cmdlist      Select by command name. This selects the processes whose executable 
                name is given in cmdlist.

例如,

if ps -C node > /dev/null
then
        date > /etc/nodeCheck.log 
else
        date > /dev/null 
fi

if pgrep node > /dev/null
then
        date > /etc/nodeCheck.log 
else
        date > /dev/null 
fi

这篇关于shell脚本来检查,如果进程正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆