检查jar是否从shell运行 [英] Check if jar running from shell

查看:215
本文介绍了检查jar是否从shell运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java jar程序,我试图在我的机器启动时运行。理想情况下,shell脚本将每隔60秒检查一次,以确保jar正在运行。如何检查jar是否在centos上运行,这似乎不起作用?

I have a java jar program that I am trying to run on startup of my machine. Ideally, the shells script will check every 60 seconds to assure that the jar is running. How do I check if the jar is running on centos, this does not appear to be working?

我当前的.sh文件:

#!/bin/bash

while [ true ]
do
    cnt=`ps -eaflc --sort stime | grep /home/Portal.jar |grep -v grep | wc -l`
    if(test $cnt -eq 3);
    then
        echo "Service already running..."
    else
        echo "Starting Service"
        java -jar /home/Portal.jar >> /dev/null &
    fi
    sleep 1m
done

我用这个用于参考到目前为止。

I used this for referencing so far.

推荐答案

根据您的程序执行的操作,可能会有或多或少的智能方法来检查它。例如,如果你有一些服务器,它会监听一个端口。

Depending on what your program does, there may be more or less intelligent ways to check it. For example, if you have some server, it will listen on a port.

然后像

netstat -an | fgrep tcp | fgrep LISTEN | fgrep :87654   # or whatever your port is

可以完成这项工作。

然后有 lsof ,它还可以检测侦听端口。

Then there is lsof, which could also detect listening ports.

最后,您可以连接并发出伪请求。例如,对于http服务器,您可以使用 lynx curl 。对于具有非stamdard协议的服务器,您可以编写一个小型客户端程序,其唯一目的是连接到服务器以查看它是否存在。

Finally, you could connect and issue a pseudo request. For example, for a http server, you could use lynx or curl. For a server with a non-stamdard protocol, you can write a small client program whose sole purpose is to connect to the server just to see if it is there.

这篇关于检查jar是否从shell运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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