我找到了这个脚本来检查程序是否正在运行。但我确实有多台服务器(它是一个火花集群)我的程序可能正在运行。
使用此脚本,我需要在运行实际程序的同一节点上运行shell脚本。然后我才能找到服务。
#!/bin/sh
SERVICE='myProgram'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
echo "$SERVICE is not running!" | mail -s "$SERVICE down" root
fi
任何人都可以帮我写一个程序来检查一个进程是否在我列出的任何一台服务器上运行?如果不是,我需要给出非零的操作系统返回码。
当shell脚本在server1上运行时,我需要ssh到node2并检查程序是否正在运行,而不是。
答案 0 :(得分:0)
这个脚本将是您问题的一个很好的起点。
您可以使用可以ssh的用户在您的某个服务器上运行此脚本
到没有passwd的其他服务器。
我想你知道~/.ssh/id_rsa.pub
和~/.ssh/authorized_keys
文件的作用。
我希望你能获得一些这个剧本所需的bash知识。
#!/bin/bash
SERVICE='myProgram'
nodes="nodeA nodeB nodeC nodeD"
rm -f err.log 2>/dev/null
for nd in $nodes ; do
rm tmp_file 2>/dev/null
ssh -o "NumberOfPasswordPrompts 0" -o "StrictHostKeyChecking no" $nd \
ps ax |grep -vw grep >tmp_file 2>>error.log
test -s tmp_file || { printf "$nd: SSH failed\n" |tee -a error.log; continue; }
if (( `grep -cw "$SERVICE" tmp_file` > 0 ))
then
echo "$SERVICE service running on $nd, everything is fine"
else
echo "$SERVICE is not running on $nd"
echo "$SERVICE is not running on $nd!" | mail -s "$SERVICE down on $nd" root
fi
done
答案 1 :(得分:0)
使用nodejs和一个名为SSH2shell的包,可以连接到多个主机,在每个主机上顺序运行不同的命令(或所有主机上的相同命令),动态创建,运行和删除bash脚本。每个主机的响应都会在使用onCommandComplete
事件运行时返回,或者在会话结束时使用onEnd
事件处理sessionText
。在主要主机断开连接时,所有主机的全文由回调或主要主机onEnd
事件处理。可以使用onCommandComplete或onCommandProcessing事件基于命令运行及其响应的任何部分添加或删除命令。
//This array of commands checks if a script already exists, if so it
//will run it and then remove it.
//If not it will printf the bash script contents to a script file
//It will then make the script exicutable using sudo chmod
//The script is run then the script is deleted.
//If you don't want to delete the file remove the last rm command.
var bashScriptHandling = ["if [ ! -f myscript.sh ]; then printf '#!/bin/bash\n" +
"SERVICE='myProgram'\n\n" +
"if ps ax | grep -v grep | grep $SERVICE > /dev/null\n" +
"then\n" +
" echo \"$SERVICE service running, everything is fine\"\n" +
"else\n" +
" echo \"$SERVICE is not running\"\n" +
" echo \"$SERVICE is not running!\" | mail -s \"$SERVICE down\"\n" +
" root\n" +
"fi'",
"sudo chmod 700 myscript.sh",
"./myscript.sh",
"rm myscript.sh"]
//Multiple host connections can be made by adding host objects to
//host.hosts array of any host.
//In this case host1 is the primary host from which all other hosts
//are connected in a number of possible arrangements.
//Host1 runs a ssh command to connect to host2, the commands for that
//host are run and then any hosts in its host.hosts array are connected
//to. Host2 connects to host3 and host3's commands are run.
//Then each host connection is terminated storing host session text
//in host1's sessionText passed to the callback function or onEnd
//event. Host3 handles its own sessionText using the onEnd event.
host3 = {
//set the connection options here
server: {
host: hostthree.com,
port: 22,
userName: user3,
password: password3
},
//set commands here
commands: bashScriptHandling,
//Host3 handling of its own sessionText using onEnd event
onEnd: funtion( sessionText, sshObj ){
this.emit('msg', "Host 3 session text:-------------\n\n" +
sessionText)
}
}
host2 = {
server: {
host: hosttwo.com,
port: 22,
userName: user2,
password: password2
},
commands: bashScriptHandling,
hosts: [host3]
}
host1 = {
server: {
host: hostone.com,
port: 22,
userName: user,
password: password1
},
commands: ["msg:primary connection host"],
hosts: [host2]
}
var SSH2Shell = require ('ssh2shell');
var SSH = new SSH2Shell(host1),
callback = function( sessionText ){
console.log ( "-----Callback session text:\n" + sessionText);
console.log ( "-----Callback end" );
}
SSH.connect(callback)
这应该为您提供一个使用SSH2shell构建解决方案的框架。
请参阅SSH2shell on github,了解有关使用的所有方面的大量自述文件帮助以及大量代码示例。
请参阅我的示例中使用的test files for code examples。