LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   kill a waiting process using shellscript (https://www.linuxquestions.org/questions/linux-general-1/kill-a-waiting-process-using-shellscript-604908/)

ranmahs 12-06-2007 06:53 AM

kill a waiting process using shellscript
 
Hai friends,

I would like to know to kill a process which is running infinitely using shell script in linux.

Any help would be greatly appreciatable..

regards
Ranmahs

matthewg42 12-06-2007 07:16 AM

What you need to do it find the process ID (PID) of the process which is stuck in a loop, and then use the kill program to send it a signal which tells it to terminate.

Exactly how to identify it will depend on how the script is run.

If the script is mad executable and called like a binary (i.e. the script is invoked directly by using the name of the script file), it will be in the process list under the name of the file. For example, say the script is called "my_script", you can find it like this:
Code:

ps aux |grep my_script
You should see output something like this:
Code:

matthew  1080  0.2  0.2  4332  1508 pts/0    S    13:10  0:00 /bin/bash ./my_script
matthew  1087  0.0  0.1  2988  776 pts/0    S+  13:10  0:00 grep my_script

The second column is the process ID. In my example it is 1080.

To kill the script you use the kill program with the process ID you found, like this:
Code:

kill 1080
This asks a process to terminate. It is possible that the program will refuse to terminate, although it's not usually the case. If it does refuse to terminate, you can tell it in no uncertain terms like this:
Code:

kill -KILL 1080
This signal cannot be ignored, and will kill the program immediately. Some programs which are waiting on some blocking kernel resource might still not die for a little while, but this is rare.

matthewg42 12-06-2007 07:19 AM

Oh silly me, I omitted the much simpler thing... If the script is running in a terminal which you still have access to, you can just press control-C in that terminal.

ranmahs 12-06-2007 09:51 PM

Thanks for your replies...

But unfortunately that is not what i wanted..

i would like to identify whether the process is still running,
my process named processname may take different time to complete which depends on the input.sometimes it may run infinitely..
so i would like to kill the process if it is running infinitely after 30seconds..
Right now i have a shell script with me as follows

pl -s (path)../processname. &//running in background
sleep 30
kill $(pidof processname)


But this code will take 30 seconds eventhough my process complete before that particular time...
now i want to kill the process either after 30 seconds or soonafter it completes...,which ever comes first.

i found the following code from another thread,..

while ($!) // while the process is still running
kill -SIGINFO $! // request info from the process
sleep 1000 // wait a second
wend

But not workinng...


regards
Ranmahs

matthewg42 12-07-2007 01:39 AM

Oh I see. Try something like this:
Code:

#!/bin/bash

# Ask for job control notifications immediately
set -o notify

# here be my command...
cmd="sleep ${1:-10}"
echo "executing cmd: $cmd &"
eval $cmd &

timeout=5
while true; do
    jobs %1 >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        # job still runs
        if [ $timeout -gt 0 ]; then
            echo "job still runs... $timeout seconds left..."
        else
            echo 'job still runs after timeout expire.  Die job DIE!'
            kill %1
            break
        fi
    else
        echo "Job terminated with $timeout seconds left to wait..."
        break
    fi
    let timeout-=1
    sleep 1
done

echo "Now I can continue with my other duties..."
wait

The important term to know is job control. There is a section in the bash manual page for it - search for the upper-case words JOB CONTROL to find this section.

Related shell commands:
  • bg
  • fg
  • jobs
  • kill
  • disown

Another good term to search for the jobspec. Backgrounded (and stopped) tasks may be referred to by their PID, but from the shell which they are started from, also a jobspec. This is %1, %2 etc. You can also say %% for the last job you started (although this might change if the last job ends). You can also use name prefixes... Anyhow, I'll leave it to you to read up on the subject.

ranmahs 12-07-2007 03:56 AM

That was awesome, great.....
I am running short of adjectives to praise your help...
Thats xactly wht i wanted...!
Thanx a lot...
Regards
Ranmahs:)


All times are GMT -5. The time now is 03:34 PM.