AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.

 View Only
Expand all | Collapse all

Service in AIX starts but won't stop

Archive User

Archive UserTue August 30, 2016 11:40 AM

  • 1.  Service in AIX starts but won't stop

    Posted Tue August 02, 2016 03:08 PM

    Originally posted by: EricFetzer


    I'm not sure I did this right because there aren't a lot of examples on the web.  Here's what I did to run my jar file as a service:

    Wrote a script to start the service like this:

     

    #!/bin/ksh

    trap "" HUP INT QUIT TSTP STOP
    trap "stopService" TERM

    stopService()
    {
        #cleanup stuff (nothing to clean up)
        exit 0
    }

    while true
    do
        #Start jar
        cd /opt/MyFolder
        /usr/java/jre/bin/java -classpath lib/log4j-1.2.17.jar:lib/ojdbc6-11.2.0.2.0.jar:./myJar.jar:./ Main deploy/myPropFile.properties
    done

    Then create the service with:

    mkssys -s MyService -p /opt/MyFolder/startMyService.sh -u $(id -u myUser) -S -n 15 -f 9 -R -Q

    Then start the service with:

    startsrc -s MyService

    It starts ok, but won't stop with:

    stopsrc -s MyService

    It claims it's stopping but I the process never stops.  It seems I need something more in my stopService method in the script but I'm not sure what.  Do I have to kill the process in there possibly?

     

    Thanks,

    Eric

     



  • 2.  Re: Service in AIX starts but won't stop

    Posted Tue August 09, 2016 03:50 PM

    Originally posted by: EricFetzer


    Nobody knows how to do this?  Is there a better place to take this question?



  • 3.  Re: Service in AIX starts but won't stop

    Posted Tue August 09, 2016 05:19 PM

    Originally posted by: AncientAIXer


    Your trap syntax is wrong.  Decided which signals you want to trap to run the stopService function:

    $ cat /usr/include/sys/signal.h|more
    …..
    …..
    #define SIGHUP     1    /* hangup, generated when terminal disconnects */
    #define SIGINT     2    /* interrupt, generated from terminal special char */
    #define SIGQUIT    3    /* (*) quit, generated from terminal special char */
    #define SIGILL     4    /* (*) illegal instruction (not reset when caught)*/
    #define SIGTRAP    5    /* (*) trace trap (not reset when caught) */
    #define SIGABRT    6    /* (*) abort process */

     

    then:

    trap stopService SIGHUP SIGQUIT SIGKILL

    http://www.ibm.com/developerworks/aix/library/au-usingtraps/

     



  • 4.  Re: Service in AIX starts but won't stop

    Posted Fri August 19, 2016 02:10 PM

    Originally posted by: EricFetzer


    Well, the service still doesn't stop.  No errors now, but the shell script is still running after I try to stop it.  New code is:

    #!/bin/ksh
    

    trap "" SIGHUP SIGQUIT SIGKILL
    trap "stopService" TERM

    stopService()
    {
        #cleanup stuff
        exit 0
    }

    while true
    do
        #Start jar
        cd /opt/ROAD
        /usr/IBM/fix/HTTPServer/java/jre/bin/java -classpath lib/log4j-1.2.17.jar:lib/ojdbc6-11.2.0.2.0.jar:./Road
    Agents.jar:./ Main deploy/ROSSApplicationServerAgent.properties
    done

     

    It seems like I should have a pid file, a kill command or the like in the stopService method, I also need to stop the java process this guy creates...  Any help?

     

    Thanks

    Eric



  • 5.  Re: Service in AIX starts but won't stop

    Posted Mon August 22, 2016 09:27 AM

    Originally posted by: EricFetzer


    Anything?  Trouble shooting strategy even?



  • 6.  Re: Service in AIX starts but won't stop

    Posted Wed August 24, 2016 11:59 AM

    Originally posted by: AncientAIXer


    Your syntax is still not correct.  The trap "stopService" TERM entry may not do anything.  The signals are:

    Signal Name Signal value Effect
    SIGHUP 1 Hangup
    SIGINT 2 Interrupt from keyboard
    SIGKILL 9

    Kill signal

    SIGTERM 15 Termination signal
    SIGSTOP 17, 19, 23 Stop the process

     

    The trap "" SIGHUP SIGQUIT SIGKILL tells the script to ignore those signals.

    From IBM documentation:

    A cancel action stops the subsystem after the subsystem's resources are released and after a grace period. This grace period is specified in the subsystem object class. The cancel stop is used only for subsystem stops and is always sent to the subsystem as the SIGTERM signal. The subsystem should catch this signal, perform subsystem clean up operations, and end. If the subsystem does not end within the wait time period, specified in the subsystem object class, the subsystem is sent a SIGKILL signal to ensure that the subsystem stops.

    You can either change TERM to SIGTERM, or specify all signals, like:

    trap "stopServer" SIGHUP SIGTERM SIGKILL SIGSTOP

    to guarantee the function runs no matter the signal.

     



  • 7.  Re: Service in AIX starts but won't stop

    Posted Wed August 24, 2016 02:32 PM

    Originally posted by: EricFetzer


    OK, we appear to be getting closer.  I now have it starting the service (it calls a shell script that kicks off a java process).  Then when I stop the service, it kills the shell script (which really wasn't doing anything anyway).  I need it to also kill the java process.  I thought I would accomplish that with my kill command but it's just killing the shell script.  Here's the code:

    #!/bin/ksh
    
    trap "stopService" SIGTERM SIGHUP SIGQUIT SIGKILL
    
    stopService()
    {
        #cleanup stuff
        kill -15 `ps -ef | grep $CHILD|awk ' $2 ~ /'$CHILD'/ {print $2}'|tr '\n' ' '`
        exit 0  
    }
    
    while true
    do
        #Start jar
        cd /opt/MyApp
        java -classpath lib/log4j-1.2.17.jar:lib/ojdbc6-11.2.0.2.0.jar:./MyApp.jar:./ Main deploy/MyApp.properties
    done
    

    Here's what the processes look like after start:

         was  9437416  2359492   0 12:18:17      -  0:00 /bin/ksh /opt/MyApp/startMyApp.sh

         was 10879170  9437416   0 12:18:17      -  0:02 java -classpath lib/log4j-1.2.17.jar:lib/ojdbc6-11.2.0.2.0.jar:./MyApp.jar:./ Main deploy/MyApp.properties

     

    Then after I stop the service:

     

         was 10879170        1   0 12:18:17      -  0:02 java -classpath lib/log4j-1.2.17.jar:lib/ojdbc6-11.2.0.2.0.jar:./MyApp.jar:./ Main deploy/MyApp.properties

     

    So instead of the child process getting stopped, it got taken over by the init process.

     



  • 8.  Re: Service in AIX starts but won't stop

    Posted Thu August 25, 2016 03:08 PM

    Originally posted by: EricFetzer


    Is the issue that stopService isn't getting hit by SIGSTOP?



  • 9.  Re: Service in AIX starts but won't stop

    Posted Thu August 25, 2016 04:10 PM

    Originally posted by: EricFetzer


    Nope, that didn't seem to help, same results.  Anyone have any ideas?



  • 10.  Re: Service in AIX starts but won't stop

    Posted Fri August 26, 2016 09:31 AM

    Originally posted by: EricFetzer


    If it helps, here is how it's done in Linux:

     

    kill -TERM $(ps xao pid,ppid,pgid | grep $(<"$pidfile") | awk '{$1=$1;};1' | cut -d' ' -f1)
    

     

    This gathers the Process Group ID and kills everything from the process group (parent as well as child processes).  Does AIX have the equivalent?  Thanks - Eric



  • 11.  Re: Service in AIX starts but won't stop

    Posted Tue August 30, 2016 11:40 AM

    Originally posted by: EricFetzer


    Are you all stumped?



  • 12.  Re: Service in AIX starts but won't stop

    Posted Wed August 31, 2016 08:15 AM

    Originally posted by: AncientAIXer


    Where is $CHILD defined?



  • 13.  Re: Service in AIX starts but won't stop

    Posted Fri September 02, 2016 09:19 AM

    Originally posted by: EricFetzer


    I found this page that described my exact conundrum and resolved it.  Seems that they had the same situation and this is how they resolved it.  I see where they defined $CHILD now so let me try it.  Thanks!

     

     



  • 14.  Re: Service in AIX starts but won't stop

    Posted Fri September 02, 2016 09:21 AM

    Originally posted by: EricFetzer


    Still, though, with a normal shutdown (trap bye-bye), they don't do a thing and it works for them.



  • 15.  Re: Service in AIX starts but won't stop

    Posted Tue September 06, 2016 02:38 PM

    Originally posted by: AncientAIXer


    The example you linked starts vmstat in the background.  The "CHILD=$!' saves the PID of the last command started in the background to $CHILD.  Being in a while loop, the script is probably starts the java process and waits for it to end.  You need to find the PIDs that have a PPID of the script.  To get the PID of the script, you can use the built-in variable $$.  Then find the processes with a PPID of the script's PID and stop them.

     

    From your previous question about the ps command, AIX ps does not have an x flag.  I don't think you need that in this case because the process probably does not have a tty associated with it.  The other flags work, but probably not like you want.  The a flag only shows the processes associated with the current process (shell).  On AIX when a process spawns child processes the PPID of the children and the PGID of all of the processes usually equals the PID of the parent process.  You can use an e flag to show everything, but distinguishing processes would be difficult without the args field.  To limit to a specific user, use the u flag.



  • 16.  Re: Service in AIX starts but won't stop

    Posted Fri September 07, 2018 09:44 AM

    Originally posted by: T.Rex


    A final solution, using "wait" and a file for the PID of sub-process "buildlet" is:

     

     #! /usr/bin/bash
     #set -x
     sigquit()
     {
     #   set -x
        date
        PID=`cat /go/bin/buildlet.pid`
        echo "kill buildlet PID: $PID"
        kill -9 $PID
        echo "buildlet stopped"
        exit 0
     }
     
     trap 'sigquit' USR1
     trap 'sigquit' USR2
     trap 'sigquit' TERM
     
     date
     echo "buildlet started pid: $$"
     
     while true
     do
             /go/bin/buildlet -coordinator=farmer.golang.org -reverse-type=host-aix-ppc64-osuosl -reboot=false &
             
    PID=$!
             echo "buildlet PID: $PID"
             echo $PID > /go/bin/buildlet.pid
             
    wait $PID
     done
     
     date
     echo "End of buildletloop. Never reached."
     exit 0
    

     

    
    mkssys -p /go/bin/buildletloop -s gobuildlet -u 0 -e /go/bin/buildletloop-errors -o /go/bin/buildletloop-output -R -Q -d -S -n 30 -f 31
    
    # kill -l
    ...
    27) SIGMSG      28) SIGWINCH    29) SIGPWR      
    30) SIGUSR1     31) SIGUSR2
    ...
    
    kill -USR1 pid
    kill -INT  pid
    kill -QUIT pid
    
    startsrc -s gobuildlet
    stopsrc  -s gobuildlet
    lssrc    -s gobuildlet
    
    lssrc -S -s gobuildlet
      #subsysname:synonym:cmdargs:path:uid:auditid:standin:standout:standerr:action:multi:contact:svrkey:svrmtype:priority:signorm:sigforce:display:waittime:grpname:
      gobuildlet:::/go/bin/buildletloop:0:0:/dev/console:/go/bin/buildletloop-output:/go/bin/buildletloop-errors:-R:-Q:-S:0:0:20:30:31:-d:20::
    


  • 17.  Re: Service in AIX starts but won't stop

    Posted Fri September 07, 2018 09:50 AM

    Originally posted by: EricFetzer


    Holy delayed reaction Batman!  We migrated all of our AIX machines over to RedHat, lol.  But thanks T. Rex!