I am seeing 2 issues with my perl script that looks like below.
Issue 1 - My if loop conditions that has "==" "." "!=" etc are failing in the below. Issue 2 - the ps -p $pid -o etime= throws an error "sh: line 1: -o: command not found"
What I am trying to achieve - to check if a process is already running, if the particular process is already running then: 1. Has it been running for more than 40 mins? If yes, kill the process. 2. Has it been running for 30-40 mins? If yes, just notify. 3. If it has been running for less than 30 mins exit.
Could some one please let me know what is the issue due to which the above said 2 issues are being noticed?
if (`ps -ef | grep example | grep -v grep`) {
print "Process is already running\n";
my $pid = `ps -ef | grep example | grep -v grep | awk '{print \$2}'`;
if (`ps -p $pid -o etime= | sed -e "s/:/\\n/g" | wc -l | grep 3`) {
print "1.Running for more than 40 mins\n";
`ps -ef | grep example | grep -v grep | awk '{print \$2}' | xargs kill -9`;
}
elsif (`ps -p $pid -o etime= | sed -e "s/:/\\n/g" | wc -l|grep "2"`) {
my $pmin = `ps -p $pid -o etime= | awk -F: '{print \$1}'`;
if ($pmin < 30) {
print "Process running for 15 mins, hence exiting";
exit;
}
elsif ($pid >= 40) {
print "2.Running for more than 40 mins\n";
`ps -ef | grep example | grep -v grep | awk '{print \$2}' | xargs kill -9`;
}
else {
print "Process running for 30 mins. Notify";
}
}
}
Aucun commentaire:
Enregistrer un commentaire