AIX subsystem stops partially

I have a shell script and I decided to create AIX service with subsystem.
. Here how the subsystem is defined:

    mkssys -s mytest -p "/home/tester/testServices/bin/test/script.sh" -a "start" -u 206 -R -Q -d -S -n 9 -f 15

Now the `script.sh` is the following script:

#!/bin/bash

PRINT_SCRIPT=/home/tester/testServices/Screen.sh
LOG_FILE="/home/tester/testServices/bin/test/log.txt"

echo "Begin script" >> "$LOG_FILE"

trap sigquit 9 15
sigquit()
{

set -x

  echo "Stop case" >> "$LOG_FILE"

PID=ps -ef | grep screen.sh | grep -v grep | awk '{print $2}'
kill -9 $PID
exit 0
}

case "$1" in

start)
echo "Starting screen.sh" >> "$LOG_FILE"
$PRINT_SCRIPT

    ;;

stop)
echo "Stopping screen.sh" >> "$LOG_FILE"
PID=ps -ef | grep screen.sh | grep -v grep | awk '{print $2}'
kill -9 $PID
;;
*)
echo "USAGE " >> "$LOG_FILE"

    echo "Usage:  {start|stop}" >&2
    exit 1
    ;;

esac
exit 0

I have made the subsystem to be used under the user with ID=206, and i have added in /etc/sudoers permissions to start/stop the service .Interestingly when I start it via `sudo startsrc -s mytest` , the subsystem goes to active status (via lssrc -a) and the start) clause gets called and i see indeed a logging in the file, however when i stop the subsystem via `sudo stopsrc -s mytest` the subsystem goes from active to inoperative ,However I don't see any logging in the file and the underlying script $PRINT_SCRIPT keeps running .Now my theory is that it doesn't even get called since even "Begin script" is not logged into the file on stop.

Interestingly when i make the same service but change the user from 206 to 0 (aka root) and start and stop it as a root everything works perfect.Why under the user with ID=206 it doesn't log anything?And is this the correct way a shell script to understand from the AIX SRC that it has to stop, if not what is the practice to make a service understand that it has to stop , as you can see I have checked if "stop" arguments is passed and also tried with signals in the sigquit() but it doesn't work under non-root user.Also under root user, on stop it doesn't log anything but the script stops working.

Normally when people experience a problem between running a script successfully as one user, and then having issues with the same script under another user, the issues are generally resolved by:

  • Make your you use full path names in all scripts and commands in your scripts.
  • Make sure any environmental variable you use in your scripts are the same for both users.
  • Make sure all users have appropriate read/write/execute file permissions.

This "different users" problems we see generally are easy to resolve if when folks start with the three bullet points above.

The most command mistakes in similar situations across all OS over the years is caused by not using full paths for files and commands.

Once you connect as root, wherever root writes (e.g your log file ) very often he becomes the owner and your user (e.g. 206) is doomed either you create a group that they share in common and both can write or you must stop using root to test...

One could assign the logfile to the user as well....

touch "$LOGFILE"
chown 206 "$LOGFILE" 

Since root can write anyway, this would help to ensure that this specific user can write to the logfile as well.

Written on mobile phone in train.

Just an idea.
Hth

In addition, ensure that kill is only run with arguments.

[ -z "$PID" ] || kill -9 $PID