Operating System - HP-UX
1753758 Members
4740 Online
108799 Solutions
New Discussion юеВ

Trap in Korn shell scripts

 
SOLVED
Go to solution
KapilRaj
Honored Contributor

Trap in Korn shell scripts

Folks,

I have a scheduled job which will run as follows,

script1 calles script2,script2 calls script3 and ends.

I opened an earlier thread where I mentioned someone (may be the OS or scheduler) is sending USR1 signals to some of the shell scripts.

So I placed a trap for script1 as ,

trap 'echo "Dont kill me"' USR1

This worked. But the subshells are still getting killed by the USR1's . Is there a away I can set a trap in a script which will be applicable for all the scripts that are triggered by that script?.

Regds,

Kaps
Nothing is impossible
8 REPLIES 8
Bill Hassell
Honored Contributor

Re: Trap in Korn shell scripts

The trap command is unique to each script (and function), so you'll need to include the trap in each script. See the comments about traps in the ksh man page.


Bill Hassell, sysadmin
RAC_1
Honored Contributor

Re: Trap in Korn shell scripts

Why spawn sub-shells?? Can you do as follows.
{ cm1 ; cm2; } --> subshells commands.

There is no substitute to HARDWORK
KapilRaj
Honored Contributor

Re: Trap in Korn shell scripts

Thank you... But don't you think it is much of a pain ? , I have some shell scripts in which, I dynamically create some shell scripts, execute them and then delete them. I need a way to trap the whole chain .. Help me guys ..

Do you know why USR1's default action is terminate for a korn shell ? .


Regds,

Kaps
Nothing is impossible
RAC_1
Honored Contributor

Re: Trap in Korn shell scripts

You can also replace all your subshell commands as follows.

sh -c ""trap 'echo "Dont kill me"' USR1";sub_shell_command1;sub_shell_command2"

Anil
There is no substitute to HARDWORK
RAC_1
Honored Contributor

Re: Trap in Korn shell scripts

Try this.
sh -c ""trap 'echo "Dont kill me"' USR1";sub_shell_comman1"

Anil
There is no substitute to HARDWORK
Bill Hassell
Honored Contributor

Re: Trap in Korn shell scripts

> Thank you... But don't you think it is much of a pain ? , I have some shell scripts in which, I dynamically create some shell scripts, execute them and then delete them. I need a way to trap the whole chain .. Help me guys ..

You need to find the source of the kill -SIGUSR1 commands. The operating system never does this automatically. Check all your cron jobs to find the culprit. There is nothing normal about kill -16 being sent to selected processes. NOTE: if kill -16 was sent to every process, your system would immediately crash or hang, so there is something selective about the kill signals. I would look for scripts that have kill commands, and rewrite any that use ps piped to grep. It is not very common to use SIGUSR1 (or SIGUSR2) so I suspect some local script writing is the problem.

> Do you know why USR1's default action is terminate for a korn shell ? .

Standard Unix behavior. See the man page for kill(1)


Bill Hassell, sysadmin
Biswajit Tripathy
Honored Contributor

Re: Trap in Korn shell scripts

Kaps Wrote:
> Do you know why USR1's default action is
> terminate for a korn shell ? .

Because SIGUSR1 (and SIGUSR2) are meant to be
user defined signals. System never sends those
signals. The intention is, if you are developing a
software that creates multiple processes and you
need to have a machanism for them to
communicate, you could use SIGUSR1 and
SIGUSR2. Typically, one of your process should
send the signal and the recipient should write a
handler that handles it. If you don't have a signal
handler but still receive those signals, the system
has no idea what to do. So killing the process is an
obvious choice for default action.

As others have already suggested, you need to
figureout who is sending you the SIGUSR1 and
why?

- Biswajit
:-)
Laurent Menase
Honored Contributor
Solution

Re: Trap in Korn shell scripts

if you replace your trap "command" SIGUSR1 by a
trap "" SIGUSR1
then the signal will be sigignored, and the sigignore is inherited form parent to child
process.