LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-06-2007, 07:53 AM   #1
ranmahs
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Rep: Reputation: 0
Unhappy 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
 
Old 12-06-2007, 08:16 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.
 
Old 12-06-2007, 08:19 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.
 
Old 12-06-2007, 10:51 PM   #4
ranmahs
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Unhappy

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
 
Old 12-07-2007, 02:39 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.
 
Old 12-07-2007, 04:56 AM   #6
ranmahs
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Talking

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


Reply

Tags
kill, process, ps


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My browser, all day today: 'waiting for linuxquestions.org...' ..waiting.. waiting .. GrapefruiTgirl LQ Suggestions & Feedback 18 05-25-2007 06:35 AM
waiting for a child process to finish execution in C cynthia_thomas Programming 2 05-26-2006 04:58 AM
cannot kill process (kill -9 does not work) mazer13a Linux - General 1 05-27-2005 03:32 PM
can't kill the process ust Linux - Software 1 09-15-2004 07:05 AM
kill process dvong3 Linux - Newbie 8 07-08-2003 10:27 PM


All times are GMT -5. The time now is 05:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration