Shell script

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?
ittechlabLinux SupportAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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 SupportAuthor Commented:
would you be able to provide the script lines so i can test it?
nociSoftware EngineerCommented:
is there any nice way to stop the service..., some tools provide a "graceful" shutdown option.
5 Ways Acronis Skyrockets Your Data Protection

Risks to data security are risks to business continuity. Businesses need to know what these risks look like – and where they can turn for help.
Check our newest E-Book and learn how you can differentiate your data protection business with advanced cloud solutions Acronis delivers

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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 DeveloperCommented:
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 FavorLinux/LXD/WordPress/Hosting SavantCommented:
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 SupportAuthor 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 DeveloperCommented:
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 FavorLinux/LXD/WordPress/Hosting SavantCommented:
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 SupportAuthor Commented:
how can i fork a process with three child process? i want to test it on my lab machine.
nociSoftware EngineerCommented:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

void do_something( char * arg) {
    printf("%s\n", arg);

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

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
Child: 20505
Child: 20506
Child: 20507

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 SupportAuthor Commented:
[root@myserver# ps -ef | grep app| grep -v grep | awk '{print $2}'

how do i kill above three PIDs. i want to write it part of the stop function.
Duncan RoeSoftware DeveloperCommented:
kill $(ps -ef | grep app| grep -v grep | awk '{print $2}')

Open in new window

nociSoftware EngineerCommented:
  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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.