Hi Friends,
I am new to this forum as well as new to shell scripting.
I have a problem here and i need someone to solve this.

Let us consider there are two processes(abc & def).There is a script which kills these two processes(i.e killtheprocess abc). Here abc is the argument .
There is a automated system which detects which process is killed and it starts the process up within two minutes.This is the summary.

Now what i want is:-
The program should contain these features:_
1. It will take the PID of the two process.
2. kill the process with the help of the script(killtheprocess). The
script should contain the two process names(i.e abc & def) .
3. After the process is killed, it should be able to detect if the two particular process has come up within two minutes by the help of the automated sytem
4. If the process comes up then it will send a mail to a group of people that everything is fine .
5. if the process doesnot come up then it should send a mail to a group of people that it needs to be manually started.
6. For confirmation that the process has indeed gone down and come up the two PIDs should be different.

It is urgent . Please help me out. If soemone needs some more clarification then please reply.

Thanks

Dani AI

Generated

The simplest reliable plan is: record the current PID(s) for each named process, kill the recorded PID(s), then poll for up to 120 seconds for a newly started PID that is different from the old one. That meets ’s requirement to compare PIDs, avoids brittle runtime-based matching suggested by , and fixes the common grep pitfalls mentioned by ; ’s ps -C idea can work too but pgrep/pidof are simpler and less error-prone across shells.

A compact example (edit PROCS and TO at the top):

#!/bin/bash
TO="ops@example.com"
PROCS=("abc" "def")
TIMEOUT=120 INTERVAL=5

declare -A OLD
for p in "${PROCS[@]}"; do
  OLD[$p]=$(pgrep -x "$p" | tr '\n' ' ')
done

for p in "${PROCS[@]}"; do
  for pid in ${OLD[$p]}; do [ -n "$pid" ] || continue; kill "$pid" 2>/dev/null || true; done
done

end=$((SECONDS+TIMEOUT)); ok=1
while [ $SECONDS -lt $end ]; do
  cnt=0
  for p in "${PROCS[@]}"; do
    new=$(pgrep -x "$p" | tr '\n' ' ')
    for np in $new; do
      case " ${OLD[$p]} " in *" $np "*) ;; *) cnt=$((cnt+1)); break ;; esac
    done
  done
  [ $cnt -eq ${#PROCS[@]} ] && ok=0 && break
  sleep $INTERVAL
done

if [ $ok -eq 0 ]; then
  echo "All processes restarted with different PIDs" | mailx -s "Processes OK" "$TO"
else
  (echo "One or more processes FAILED to restart in $TIMEOUT seconds"; for p in "${PROCS[@]}"; do echo "$p old:${OLD[$p]} new:$(pgrep -x "$p" | tr '\n' ' ')"; done) | mailx -s "Manual start needed" "$TO"
fi

Notes and cautions: run as a user with permission to kill the targets; prefer kill (TERM) before -9; ensure a working MTA (sendmail/mailx) on the host or switch the mail lines to use an external SMTP client or a Python script. If PID-reuse is a worry on very busy systems, verify process start time (via /proc/<pid> or service tooling) in addition to PID. If processes are managed by systemd, prefer systemctl checks and notifications rather than raw PID polling. For debugging, add set -x and check mail and system logs.

Recommended Answers

All 4 Replies

sounds like home work, please try to attempt to write the script then we can help you out

well the ps command returns the running time. so something like ps -ax|grep 2:00 will give you the process that has been running for 2 minutes, as well as the PID and the filename
after that take the first 4 simbols from the ps output (the PID) and kill that process

Agreed,

If you have something, post it and we can help you make it work better.

For a general "is it up at all", just us ps and grep, much like above:

$isitup=`ps -ef|grep "[a]bc"`

and test that string to determine if your process is running at all.

Best wishes,

, Mike

I feel thsi way it can be done,
ps -C <process_name>
redirect this output to any file
then use while loop use head and tail command so that process can be killed one by one.
Then give sleep 120 seconds
then use the same process but use if loop to send the message
try write the code so that we can correct you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.