error situation handling when starting app from a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting error situation handling when starting app from a script
# 1  
Old 09-05-2012
error situation handling when starting app from a script

Hi,
in my bash script I start 3rd party app that runs quickly normally and returns some data that I use later for processing. However if there are problems the app doesn't return anything and just hangs... then my script also hangs of course, awaiting for the app to comeback. Was wondering how to avoid that so the script could carry on or at least bailout with an error if that happens. Is there a way to do it in bash ?

Code:
get_proc_stats() {
     port=$1
     tstats=`app $port`
}

I was thinking maybe I could start/fork seperate process in background which would then check the main process somehow, maybe sending a signal USR1 and then the main process would be maybe writing something to some temp. file and forked process runnig in background would be checking if update to temp. file happend, if not then it could wait some time and kill the main process ?
What do you think?

Any hints on that appreciated,
Cheers,
Tomasz


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 09-05-2012 at 10:54 AM.. Reason: code tags
# 2  
Old 09-05-2012
Cleanest way would be to find out what is producing this hang.
You can always have another script or function that checks for the process. Though you might want to have something like a kind of deadline in there, so there can be "measured" when it is time to kill the process like a time threshold for example.
But something like that should be a workaround if the investigation didn't work or takes too long.
# 3  
Old 09-08-2012
yes, we have got a patch and now the app seem to work stable.
But this topic still is interesting how to achieve in a shell script. In popular languages you can do things just like:
Code:
if (trysys(app))
   code if app fails
else
  proceed with the script

Maybe the simplest way would be to start a subshell (by using &)and run that app in there, the main script would then loop around some time checking the PID of a subshell from that script, or just would wait some time for the subshell to finish - is there a way to check subshell is running or finished ? And then based on that I could proceed in main script. I guess that must be doable in bash. If anyone has done something like that and don't mind sharing I would welcome a template with syntax

Cheers,
Tomasz

Moderator's Comments:
Mod Comment edit by bakunin: Please view this code tag video for how to use code tags when posting code and data.

Last edited by bakunin; 09-08-2012 at 06:47 PM..
# 4  
Old 09-08-2012
When you send a process in the background you can use the "jobs" command to get its PID. Using this PID you can terminate it by sending signal 15 (or, if this doesn't help, 9).

The display format of "jobs" might differ slightly from OS to OS, so you will have to fine-tune the following sketch to work on yours. I have not tested it yet, so you might have to adapt it a bit too:

Code:
....

controljob ()
{
typeset    chJob="$1"           # some command line
typeset -i iTimeOut=$2          # time to wait in seconds
typeset -i iPID=0
typeset -i iCnt=0

$chJob &
iPID=$(jobs | sed, awk, ...)    # whatever is necessary for your system

while [ $iCnt -lt $iTimeOut -a $(ps -p $iPID | wc -l) -gt 0 ] ; do
     sleep 1
     (( iCnt += 1 ))
done

if [ $(ps -p $iPID | wc -l) -gt 0 ] ; then
     kill -15 $iPID
     sleep 3                    # the process may need some time to terminate
fi

if [ $(ps -p $iPID | wc -l) -gt 0 ] ; then
     kill -9 $iPID
     exit 1
fi

exit 0
}

Moderator's Comments:
Mod Comment finally i'd like to ask you not to ignore requests by the moderators again. zaxxon has asked you to use CODE-tags and in your next post you didn't use them again. Thank you for your consideration.


I hope this helps.

bakunin

Last edited by bakunin; 09-08-2012 at 07:13 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Wuhan Coronavirus Status App for China - Rapid Prototype using MQTT and the IoT OnOff IOS App

With a little bit of work, was able to build a nice "Wuhan Coronavirus Status" app using MQTT and the IoT-OnOff app. More on this technique here: ESP32 (ESP-WROOM-32) as an MQTT Client Subscribed to Linux Server Load Average Messages The result turned out nice, I think. I like the look and... (10 Replies)
Discussion started by: Neo
10 Replies

2. Shell Programming and Scripting

Script FTP maintain error handling

Hi, I have ftp script like below How to insert an error handling, If the transfer failed then send mail to me. Actually, I just need the script to send an email if the FTP failed. How to put the email script within FTP script? Thank You Edy (5 Replies)
Discussion started by: edydsuranta
5 Replies

3. Shell Programming and Scripting

Error handling

Hello fellow UNIX gurus :) I have a problem regarding the script below: # Variables used in this shell. power=0 # Stores squared integer total=0 # Sum of all squared integers num=0 # Stores command line arguements # Provides error handling if command line... (5 Replies)
Discussion started by: Learn4Life
5 Replies

4. Shell Programming and Scripting

Help me add Error Handling to my script

Hi all, I have some sections of a script that I am trying to add error handling to. Basically if it returns any error, just continue. This is for a deployment script that i am writing to assist in the deployment of scripts out to other systems. At the top of my KSH script i added this... (5 Replies)
Discussion started by: nitrobass24
5 Replies

5. Shell Programming and Scripting

Expect Script Error Handling

Good Day Everyone, I was hoping to get a little insight into an expect script that I've written. Basically we have this expect script to perform an sftp upload, key authentication is not an option, and sftp is the only method supported by our vendor, thus the need for this. I want to be... (3 Replies)
Discussion started by: thaller
3 Replies

6. Shell Programming and Scripting

Calling a Perl script in a Bash script -Odd Situation

I am creating a startup script for an application. This application's startup script is in bash. It will also need to call a perl script (which I will not be able to modify) for the application environment prior to calling the application. The problem is that this perl script creates a new shell... (5 Replies)
Discussion started by: leepet01
5 Replies

7. Shell Programming and Scripting

Help with Error Handling on Script

Hi, I need your guys help again. I run a script which check for some process status in a loop. when i check the process some of the process could throw an error, how can i check that inside my script. Thanks, RR (3 Replies)
Discussion started by: rrb2009
3 Replies

8. Shell Programming and Scripting

Issue with Error handling,not able to continue the script further

Hi, I am trying to write a script to cleanup files in a log directory .. cd log find Datk** -mtime +7 -exec rm -f {} \; 2> /dev/null Have used the above to clean up files in log directory more then 7 days older. The file can be something like ( auto-generate by some processes and... (2 Replies)
Discussion started by: nss280
2 Replies

9. HP-UX

help auto starting app on boot

Hi I need a bit of help figuring out how to auto start an application on boot on an HPUX. I am a fairly exp AIX guy now working an HP shop. I use to change a /etc/rc... file. Any advise would be a great help TIA. –K If it is a help the program is a DB and runs as root /usr/ud{##}/startud... (3 Replies)
Discussion started by: KmJohnson
3 Replies

10. Shell Programming and Scripting

how to capture my app starting up.

This is the script for the cold backup that we get from mycat every time it goes down and up again. It's a huge email that we get and all within the body. Here is the dilemma; I would like to capture just the “”successful start up of the luminis app.”” I don't know if I need to do an “if... (1 Reply)
Discussion started by: parente
1 Replies
Login or Register to Ask a Question