UNIX for Advanced & Expert Users

Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Trap the EXIT_CODE from a script


👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 4 Weeks Ago
LoneRanger LoneRanger is offline
Registered User
 
Join Date: Jun 2014
Last Activity: 21 June 2018, 9:10 AM EDT
Posts: 24
Thanks: 3
Thanked 1 Time in 1 Post
Trap the EXIT_CODE from a script

Hi All,

I have a script which calls SQLPLUS and do some data cleanup. But sometimes the SQL hangs and the script keeps on running.
In that case, we kill the script using "kill" command, but as soon as we kill the script it exists with a non zero exit code which makes the job fail( we have a checking for non zero exit code).
this we want to avoid. Is there any way, where we can trap the exit code and change it to 0 so that it completes successfully.
I want to accomplish it without making changes in the script., I would like to do it from outside from the same unix console.

Can you please let me know if it's possible? like using the TRAP command?
Sponsored Links
    #2  
Old 4 Weeks Ago
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 20 July 2018, 11:57 AM EDT
Location: Lancashire, UK
Posts: 3,588
Thanks: 1,594
Thanked 706 Times in 633 Posts
Could you share the script, or at least a cut-down version that illustrates your issue? (perhaps with a long sleep in it)

If not, then it's a bit difficult to help.


Kind regards,
Robin
Sponsored Links
    #3  
Old 4 Weeks Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 July 2018, 2:56 AM EDT
Location: Aachen, Germany
Posts: 13,081
Thanks: 452
Thanked 4,017 Times in 3,693 Posts
I'm afraid you won't be able to do it without changing the script. Yes, you can trap e.g. the SIGTERM signal and exit gracefully, but you need to make sure you intercept all ramifications.
    #4  
Old 4 Weeks Ago
murugesandins murugesandins is offline
Registered User
 
Join Date: Jun 2018
Last Activity: 22 June 2018, 10:14 PM EDT
Posts: 7
Thanks: 0
Thanked 3 Times in 3 Posts
Good comment from Robin.


Instead of using SIGTERM you can use other signal.
Following comment is only a sample:
C or CPP code using SIGUSR1 signal
  1. when receiving SIGUSR1 it is writing information to log file
  2. executing the script using system call in child process.
  3. parent process waiting for child process
  4. when parent process receive SIGUSR1 signal, writing current status to log file.

Last edited by rbatte1; 4 Weeks Ago at 08:34 AM.. Reason: Formatted numbered list with LIST=1 tags
The Following User Says Thank You to murugesandins For This Useful Post:
rbatte1 (4 Weeks Ago)
Sponsored Links
    #5  
Old 4 Weeks Ago
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 3 July 2018, 11:43 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,786
Thanks: 112
Thanked 1,698 Times in 1,248 Posts
Quote:
Originally Posted by RudiC View Post
I'm afraid you won't be able to do it without changing the script.
I beg to differ: the script in question has to be called from somewhere to be started. Write a "wrapper script" so that your script is called which always exits with 0 regardless of what happens. Then replace the call of the original script with the wrapper script. The exit code for the original script would be reported to the wrapper script and nobody else. If this chooses to ignore it than it is ignored.

To be honest, doing it by changing the original script as RudiC suggested would be by far the cleaner and better solution still. But in case you can't change the script for whatever reason this might be a work-around.

I hope this helps.

bakunin
The Following User Says Thank You to bakunin For This Useful Post:
rbatte1 (4 Weeks Ago)
Sponsored Links
    #6  
Old 4 Weeks Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 July 2018, 2:56 AM EDT
Location: Aachen, Germany
Posts: 13,081
Thanks: 452
Thanked 4,017 Times in 3,693 Posts
Quote:
Originally Posted by bakunin View Post
I beg to differ: the script in question has to be called from somewhere to be started. . . .
Yes, that - if I read post#1 correctly - is a job, i.e. a script, program, or cron entry, calling the script in question, neither of which shalt be modified.


Quote:
Then replace the call of the original script with the wrapper script. . . .
The only way to do so would be to rename the original script and put a wrapper script with its name in its place, and then call the new name - horrifying picture to be documented.


But - as long as we don't get additional context info all this is sheer guesswork.

Last edited by RudiC; 4 Weeks Ago at 07:51 AM..
The Following User Says Thank You to RudiC For This Useful Post:
rbatte1 (4 Weeks Ago)
Sponsored Links
    #7  
Old 4 Weeks Ago
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 20 July 2018, 11:57 AM EDT
Location: Lancashire, UK
Posts: 3,588
Thanks: 1,594
Thanked 706 Times in 633 Posts
Of course, renaming the original might cause problems. I had to intercept calls to usermod etc. on an old HPUX server once so we could log what was being called. The original (compiled) code was hard-linked for several functions (usermod, useradd, etc.) and reacted differently depending on what name was used to call it.


Perhaps we could suggest many ways of accomplishing this (e.g. with trap or other coding changes) but until we can see what is being called, we're all going to be stuck.


Assuming something checks the value of $? then you could simply add a : or true after the call that may get killed off. Of course, then you don't know when it may have genuinely failed. We need to see (enough of) your code to give you something useful.




Kind regards,
Robin
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
VM trap may work differently than a pure install trap. newuser45 Homework & Coursework Questions 2 05-20-2013 10:14 AM
SCRIPT TO TRAP ILLEGAL COMBOS gimley Shell Programming and Scripting 2 03-08-2011 10:33 AM
trap ctrl c in shell script arvindng UNIX for Advanced & Expert Users 2 11-28-2008 10:45 AM
Cntl+z Trap is not detecting ??? Help required to add a trap detection ??? frozensmilz Shell Programming and Scripting 1 10-08-2008 06:15 AM
Trap key press in a script praveenbvarrier Shell Programming and Scripting 3 04-05-2008 04:56 PM



All times are GMT -4. The time now is 05:26 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?