Killing a bash process and running the second part of script


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Killing a bash process and running the second part of script
# 1  
Killing a bash process and running the second part of script

I want to run a script that calls remote ssh and if it gets hung, I want to be able to kill that part of the script and run another command in the script

for example I have a statement like this:

Code:
if [[ $nbserve == SENDone || $nbserve == Zray|| $nbserve == Partition ]]; 
then 
    export tapes=$(for tape in $(su - nacct -c 'ssh remote1 "cat /filer/cart/vol0/tech/File\ Lists/'"$nbserve"'/'"$move"'"'| gawk '{print $1}'|sed -n '/\([0-9]\)\(.*\)/p'); 
    do echo $cartridge;done);
else 
    export cartridges=$(for cartridge in $(su - nbacct -c 'ssh remote1 "cat /filer/cart/vol0/techFile\ Lists/'"$nbserve"'/'"$movelist"'"'| tr -d " \t" |sed -n 's/\(.*)\)\([A-Z]\{1,3\}[0-9]\{1,5\}\)\(.*\)/\2/p'); 
    do echo $cartridge;done);
fi


90% of the time this command works, but sometimes the nbserv name is not accessible this way (by ssh). In that case, I'd like to run another command that uses another syntax.


Is there a way to terminate the above statement if it hangs, and then proceed to move to the next portion of the script to run another command?
Something like a kill process command. But then how would the script go to another line? Is it possible?

Last edited by vbe; 09-02-2014 at 12:56 PM.. Reason: No colour please! - Please send code properly formatted!
# 2  
Let's take this one run-on one liner and make it usable. Cramming stuff into one
line may be cool but it prevents doing what you want.

Code:
    # old 
    export tapes=$(for tape in $(su - nacct -c 'ssh remote1 "cat /filer/cart/vol0/tech/File\ Lists/'"$nbserve"'/'"$move"'"'| gawk '{print $1}'|sed -n '/\([0-9]\)\(.*\)/p'); 
    do echo $cartridge;done);

Code:
    myfile="/filer/cart/vol0/tech/File\ Lists/$nbserve/$move"
    # many ssh calls hang change this prt to check for completion
    # Instead of many ssh calls copy the file locally.
    (su - nacct -c "scp remote:${myfile} /tmp/tmp1" ) &
    pid=$!    # pid of child scp process
    sleep 5
    kill -0 $pid
    if [ $? -eq 0 ] ; then
      kill $pid   # get rid the child process
      wait
      # we are hung up so execute some other lines here
    else
      # if we did not get hung up then run gawk on the local file /tmp/tmp1
      # here.
      tapes=$( #your gawk/sed goes here using /tmp/tmp1 # )
    fi


The concept is to use return codes to get the child pid, sleep a while,
then check the pid. If you were to sleep 5 between all of your for loop
calls this could take forever. So, we lost the loop. If you like loops you
can run it AFTER the one ssh(scp) call.
This User Gave Thanks to jim mcnamara For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #514
Difficulty: Easy
As a general rule, the more a variable is used, the longer the variable name should be.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Killing the process if running for long time in script

I am running a script which will read the data from fail line by line and call the Java program by providing the arguments from the each line. The Java code is working fast for few records and for some records its getting hanged not providing response for morethan one hour. Currently am... (4 Replies)
Discussion started by: dineshaila
4 Replies

2. Shell Programming and Scripting

Automated Script for Process killing

Hello all, I'm in need of a Script which needs to wait for all the child process to end and then kill the main process. I have a process called mainpp which runs for different instances like evpn, nge, gmn etc so when i query for mainpp process it looks like below. bash-3.2$ ps -eaf |... (6 Replies)
Discussion started by: Mahesh_RPM
6 Replies

3. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

4. UNIX for Dummies Questions & Answers

Killing a process which is running in different server

Consider two servers tst01 and tst02. i need to log in tst01 and check for the processes which run in tst02. then based on pid , i need to kill that process. is it possible to achieve? I am able to connect to tst02 using ftp. But the problem is, if i use ps it says invalid command. ... (3 Replies)
Discussion started by: pandeesh
3 Replies

5. Shell Programming and Scripting

**need help for killing a process through script**

Hello All, i hope you are fine. I need a little help from you people-- inside a script i want to kill a parent process by checking it with the child process.. p_pid=`ps -e | awk '/ra_cmd_d/ {print$1}'` here i am selecting the child process id in p_pid. next-- sleep_pid=`ps -af |... (3 Replies)
Discussion started by: onlyniladri
3 Replies

6. Shell Programming and Scripting

script not killing process!

i am using script to connect remotly to server and run some commands , one of these commands is to kill some process but tried different ways with no hope sshpass -p 'pass' ssh -o StrictHostKeyChecking=no server kill -9 `pgrep procs` getting error message "kill: bad argument count" ... (2 Replies)
Discussion started by: mogabr
2 Replies

7. Shell Programming and Scripting

Leaving Program running but killing the script

Sorry for all the threads. I am almost done. I ahve a bash script that is launching a diags program then copying the .html over my client. then it does the following line /opt/firefox/firefox report.html it launches it fines but the program waits for me to close the window or kill the script.... (2 Replies)
Discussion started by: deaconf19
2 Replies

8. AIX

killing a process from a script

Hey all. I'm brand new to this forum and am looking for some help. I have a script that verifies that the backup tapes are working correctly. Basically is uses 1 command: restore -xpqvf > rootvglog I use this for each volume group that we have. We run this everyday but the problem is, we... (4 Replies)
Discussion started by: jalge2
4 Replies

9. Shell Programming and Scripting

Killing a process from perl script.

How do I kill a process, say by name "drec" from a perl script. I tried with : ps -eaf | grep drec | awk '{print $2}' | xargs kill -9. The output I got is : ps -eaf | grep drec | awk '{print }' | xargs kill -9 /usr/bin/kill: ipgen: Arguments must be %job or process ids {But, $2 is not... (3 Replies)
Discussion started by: sharuvman
3 Replies

10. Shell Programming and Scripting

killing process using a script

can I do ps -ef | grep <process_name> and kill the process is it exists? and send a mail to me that the process was found and killed Thanks much... KS (4 Replies)
Discussion started by: skotapal
4 Replies

Featured Tech Videos