Terminate a process using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Terminate a process using shell script
# 1  
Old 05-28-2011
Question Terminate a process using shell script

Hi,

I am writing a shell script to run a process and write the output of the process to a file.

Inside program.sh:
./process.sh > tempfile
..

open tempfile
do the following

But the problem is that process.sh is running indefinitely and program.sh is not executed completely. Can anyone help me to terminate process.sh after the output has been written to the tempfile, so that program.sh can continue its execution.

Last edited by dreamgirl314; 05-28-2011 at 06:16 AM..
# 2  
Old 05-28-2011
You have to identify a condition to make sure that process.sh has completed its function. Once that is done, you can star the process.sh in background using & at the end instead of just executing it.

Then you can terminate the process using kill -9 PID
This User Gave Thanks to kumaran_5555 For This Post:
# 3  
Old 05-28-2011
Hi,
Thank you for the answer, but the problem is that I should ensure that process.sh is executed only once and only one time value should be entered in the tempfile.(Else it runs infinitely writing values to the tempfile). If I run using

./process.sh > tempfile &
sleep 5
kill -9 $!

There is no way to be determine if write has happened atleast once. Also I cannot determine how long I should wait.

Kindly help me
# 4  
Old 05-28-2011
Hi and welcome to the forum!

How do you know that the process is done writing all the output?
It seems to me that process.sh should finish after it's finished, and i'd try to modify it so that it does. But of course I don't see all the details, so here, this should work on a stubborn process that doesn't know when to leave.
Firstly, to answer my first question, I'd make sure I know when the process.sh is done writing all the output. I'd make it write a unique string that defines the end of writing, e.g. "==DONE WRITING OUTPUT==". Then i could do the following:


Code:
#!/bin/bash

out=output.log
: > $out #create the file, or truncate to zero length

./process.sh > $out & #run in the background

pid=$!  #process ID of the neverending process

tail -f $out | while read line ; do #keep reading lines from output
    echo $line | grep  "==DONE WRITING OUTPUT==" &>/dev/null 
    if [ $? -eq 0 ] ; then #grep matched the string
      kill $pid #kill process.sh
    
      tailPID=`ps ax | grep -v grep | grep "tail -f $out" | awk '{print $1}'`
      kill $tailPID #kill tail

      #it's still running here, until end of loop
    fi  
done 

#finish whatever needs to be done

If the process.sh is so stubborn that the 'kill $pid' call won't terminate it, use 'kill -s 9 $pid' instead.

There is quite a lot of killing there, so it deserves some explanation. We have the following processes running:
1) parent script
2) process.sh script running in the background
3) tail -f
4) subshell started by pipe

2) and 3) are killed directly by 'kill $PID' call.
4) is ended by when it reaches the end of loop and receives no more input from (now non-existent) tail
1) continues running until end

If the tail wasn't terminated, this script would hang, refreshing the last lines of unchanging output forever.
# 5  
Old 05-30-2011
I have small doubt here about the logic. If we check for the output file for specific flag like "==DONE WRITING OUTPUT==" It may not be possible to ensure that the process.sh has run only once.

Because if process.sh is running little faster than your shell script, it might add few more rows after starting second run. It is totally situation dependent and it shouldn't be done by this way.

Please post the process.sh script, may be we could find a work around for this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Terminate shell script after a specified time

I have a shell script that checks a file state.txt, deletes fit.bin if state.txt is empty. I cron this at 2am, I will want the script to stop by 8am irrespective of the value of state.txt, any ideas? #!/usr/bin/ksh while true; do if ] ; then echo else rm ~/fit.bin exit ... (8 Replies)
Discussion started by: aydj
8 Replies

2. Shell Programming and Scripting

Run script every minute and terminate after specified number of executions

when it runs and look at my acron.log file it generates an error as below /tmp/prog.sh: line 4: (12 Replies)
Discussion started by: azherkn3
12 Replies

3. Shell Programming and Scripting

strange behaviour script terminate before complete execution

I'm working on a script to make backup of various folder located on various host using different OS. I got a strange behaviour because the script donět process all lines of a configuration file, the script execute only one loop even the input file have 6 lines: This is the script: #!/bin/bash... (4 Replies)
Discussion started by: |UVI|
4 Replies

4. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

5. Shell Programming and Scripting

how to terminate ssh connection without terminating script

Hi all, I connect with SSH connection to remote machine in the script and ı want to logout at half of the script then continue to script. If ı write exit in the script it terminates script not SSH connection. How can i do that please help me (1 Reply)
Discussion started by: fozay
1 Replies

6. UNIX for Advanced & Expert Users

Interrupt signal Control C takes too long to terminate a process

I have a process to terminate, and when keying Control C/ kill -int , it takes 15 minutes to half an hour to terminate the process. I've tried using kill -2, or keying control c twice, however the process seem to be killed abruptly, without writing into the log file. So the only way in order to... (8 Replies)
Discussion started by: paqui
8 Replies

7. Shell Programming and Scripting

Terminate process

We have a system user "oracle_usr" always run some process in the system , but sometimes , these processes will not stop automatically until we terminate the process , can suggest the method how to terminate the process that is run by "oracle_usr" and run over 10 minutes ? thx (5 Replies)
Discussion started by: ust
5 Replies

8. Shell Programming and Scripting

terminate process

I want to have a script to terminate the system process that generated by user oracle_usr and have already processed for over 10 minutes , could suggest the script ? thx (1 Reply)
Discussion started by: ust
1 Replies

9. Shell Programming and Scripting

terminate the process

In my system , there are a system user "cronusr" , it is mainly to run the crontab job in the database .Sometimes the cronjob process will be failure ( due to some reason ) so that many cronusr process are in the system , it affect other process in the system , I want to have a script that can... (2 Replies)
Discussion started by: ust
2 Replies

10. Shell Programming and Scripting

Terminate session on completing script

Hai all.. How do i terminate my telnet session automatically when my java applicatiion exits. i have a file run which executes my java application and takes care of all class and library path settings prior to the execution. I would like to terminate my session when my application exits. The... (4 Replies)
Discussion started by: deepsteptom
4 Replies
Login or Register to Ask a Question