Terminating a process - is this code best practice?
Hi Folks -
I am building a process to kill a list of services. Sometimes, there's a service that hangs therefore I need to add an additionla peice of code to kill all instances of a service if it exists.
Here is that portion of code:
My goal with the above piece of code is to kill any instances of ESSSVR.
Signals are a way of communicating with processes. And Signal 9 is a kind of non-communicating with which the processes get stopped immediately.
Most Software implement a proper cleanup when beeing told to terminate. All that cleanup can not take place here, because SIGKILL means the processes are stopped immediately by the operating system. That can cause a lot of trouble in your application, because Junk is not cleaned up(temporary files, temporary tables, proper setting some values in database done at a clean shutdown,...). Maybe your application will not start after you killed it that way once or some more times. Maybe your applications data will be corrupted badly over time.
A better way of doing what you want is send a signal 15 ( = SIGTERM), then give the task enought time time (10-60 Seconds depending on how much your application usually needs, to avoid trouble I would suggest you rather choose the 60 seconds). If the process is still alive afterwards send a signal 2 ( = SIGINT) and wait for another 10-60 seconds. If the process is still alive afterwards, finally kill it with signal 9 (SIGKILL).
Here's one of my scripts(could be improved though), doing this:
The programs pgrep and pkill may be useful too.
Have a look at the manpage signal(7) for a detailed explanation of the standard signals.
Last edited by stomp; 10-03-2016 at 01:14 PM..
Reason: Changed the script the you do not have to wait 30 secs if the process is already gone
Is this the portion I add ESSSVR or is this just getting all processes then it find the pattern later?
PID_REGEX_PATTERN="^\s*("
No. I'm grepping the process list for multiple PIDs that may match the given pattern.
For normal use, you may just change the very last line of the script:
If you like to tweak the script, the most liked improvement will be to do not wait the full 30 second period if the process is already terminated.
I recommend you to test the script in a testing environment before using it in production, since you do not know anything about my environment and vice versa for me.
Can any help me in finding the way to close opened file descriptor in Solaris ,without killing process. As accidently a file was removed which was opened by a process.
Much thanks in advance :) (11 Replies)
Hi All,
I am trying to make a small shell script.In this it will got directory as per variable & run the find command on that directory.There are 120 + directories & not sure all of them are mounted.So the issue is if the directory doesent exists my loops gets terminated. so is there any was... (3 Replies)
Hi,
Since I staterd working as Unix sysadmin (about 3 years ago) I always used to trigger a process evaluating the conditions needed to this process to be executed. Recently I've change the company where I work, and they usually create a trigger file to start a process or to stop a process while... (1 Reply)
Hi all , i know i ask a lot of question but these are really hard to solve and important question. I send two scripts:
expect.sh:
#!/usr/local/bin/expect
spawn ssh root@172.30.64.163
expect "login:"
send "root\n"
expect "password:"
send "root\n^M"
interact
and
son.sh:
... (2 Replies)
hi all,
i m running few batch process through shell script using nohup command but when session get terminated(due to network, reboot of desktop and closing session directly) all processes terminating abnormally and core file is generating.
application batch process is connecting oracle... (4 Replies)
I now that this isnt the greatest code around. Im a network guy by trade not a programer .. but needed something to compare config files ...
Anyway ... intermittently, the program terminates.
Ive been looking at the code for a week trying to figure it out and Im stumped. Can anyone provide... (0 Replies)
Hi
I was working on a shell script with randomly shows a page of text from a randomly selected topic .As soon as the page is displayed it callers a timer script which keeps on running indefinitely until the timer script is killed by the user.
This is where I have the problem,if I press... (2 Replies)
Hi,
I'm trying to make a script that reads the console input and terminates with CTRL+D. It's absolutely basic but I don't know how to "read" the CTRL+D. I've tried a bunch of things like
EOT=^D
while //with & without quotations
do
read input
echo $input
done
while
while ]
... (12 Replies)
just like what the subject said
but the ip is different
example
if now my IP is 192.168.0.50 and my name is seed
if i wanna terminate 192.168.0.55 with the same nick of mine, seed
can i do that ?? and what is the command ?? (2 Replies)