Shell script

ittechlab
ittechlab used Ask the Experts™
on
I am working on script where i have to create a init script to start/stop the application. However the application does not stop properly. i had to kill 3 - 4 process by grepping the PID ids.

How can i do this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Sounds like your application might be detaching itself.

Detach - Close STDIN/STDOUT/STDERR + change effective user/group + associate with init (PID == 1).

If this occurs, then you'll have to scrape ps output to locate one or more processes, which you will have to explicitly KILL or HUP to completely stop.

What signals you send + how long you wait before sending a hard kill, depend on the application under control.

Some applications you can just shoot in the head. Others... like MariaDB, must be gracefully terminated to flush memory buffers to disk, else you end up with database corruption.

Maybe place an update with your application name, purpose + ps fauxww off your system, when your application is running.
ittechlabLinux Support

Author

Commented:
would you be able to provide the script lines so i can test it?
nociSoftware Engineer
Distinguished Expert 2018

Commented:
is there any nice way to stop the service..., some tools provide a "graceful" shutdown option.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

David FavorFractional CTO
Distinguished Expert 2018

Commented:
Start by providing the requested data I mentioned.

There's no way to answer "would you be able to provide the script lines so i can test it?" without having the above data.

Every Application has unique characteristics which require very different types of control scripts.
Duncan RoeSoftware Developer

Commented:
Rather than scrape ps output as per https:#a42654768, you might be able to use pkill or killall - have a look at their man pages.
David FavorFractional CTO
Distinguished Expert 2018

Commented:
As I suggested above, provide your application name, purpose + ps fauxww off your system, when your application is running.

Describing your application purpose is very important.

This is a great starting point to answer your question.
ittechlabLinux Support

Author

Commented:
I found some of the middleware application which uses java and if I grep java process it may belong to multiple application within the system.

How do i differentiate which application related to which java process?
Duncan RoeSoftware Developer

Commented:
ps axfu gives you a tree-style view of process hierarchy. Usually if you kill the top process in a tree then the rest will go away.
It should only be necessary to kill actual java processes if the have gone off the rails.
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Be sure you dig into your top Java process + ensure you understand how to kill the process without corrupting data.

Sometimes, you can just send the parent a SIGKILL (-9) + sometimes it's best to send SIGTERM (-15) + sleep for a while + send a SIGKILL.

Point is, to avoid data corruption, you'll have to understand how this process forest actually works.

You can always contact the vendor providing this code to you.

Other tricks include running lsof to determine if any files are open, which might be corrupted.
ittechlabLinux Support

Author

Commented:
how can i fork a process with three child process? i want to test it on my lab machine.
nociSoftware Engineer
Distinguished Expert 2018

Commented:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

void do_something( char * arg) {
    printf("%s\n", arg);
    sleep(300);
    exit(10);
}

int main(int argc, char **argv) {
    int i, child; 
    for (i=0; i< argc; i++) {
        if ((child = fork()) == 0) {
            do_something(argv[i]);
         } else {
            printf("Child: %d\n",child);
         } 
     }
    sleep(330);
}

Open in new window


Above C program should start a child for each of the arguments supplied... (including the program name).
all chldren sleep 300 seconds, the parent 330... so there is no synchronisation  etc. but it is the easiest code that will not harm in the long term, but allows for some action...
if you need longer times: change sleep times 300-> 600 and 330 -> 630...f.e.


$ ./a.out a b c
Child: 20504
./a.out
Child: 20505
Child: 20506
Child: 20507
b
a
c

Open in new window

in Another sessio:
$ ps axf
...
20511 pts/2    S      0:00  |   |   \_ ./a.out a b c
20512 pts/2    S      0:00  |   |   |   \_ ./a.out a b c
20513 pts/2    S      0:00  |   |   |   \_ ./a.out a b c
20514 pts/2    S      0:00  |   |   |   \_ ./a.out a b c
20515 pts/2    S      0:00  |   |   |   \_ ./a.out a b c
...

Open in new window

ittechlabLinux Support

Author

Commented:
[root@myserver# ps -ef | grep app| grep -v grep | awk '{print $2}'
30519
30626
30761

how do i kill above three PIDs. i want to write it part of the stop function.
Duncan RoeSoftware Developer

Commented:
kill $(ps -ef | grep app| grep -v grep | awk '{print $2}')

Open in new window

nociSoftware Engineer
Distinguished Expert 2018

Commented:
if:
  pidlist= $(ps -ef | grep app| grep -v grep | awk '{print $2}')

First do:
  kill -TERM $pidlist  #signal 15, each process it should stop, but may ignore
  sleep 20    #then after short while:  
  kill -KILL $pidlist   # signal 9, allow each process a harakiri, Absolutely fatal

Open in new window


The kill -KILL   will be immediately fatal, not exit handlers etc.  (The kernel does an _exit() on behalf of the process).
A TERM can be caught and is meant to signal process to stop working after flushing buffers etc

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial