Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > Alternative Computing > Linux Support
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Linux Support Linux - Operating Systems and Applications Support

Reply
 
LinkBack Thread Tools
Old 10-15-2009, 11:42 PM   #1 (permalink)
Registered User
 
Join Date: Aug 2006
Posts: 320
OS: XP Pro, Vista Business, Suse Linux, Win98 SE


Ubuntu Bash Script PID Problems

Hello all,

I'm trying to right a bash script. Part of it requires me to write a timeout function. Now... I'm not entirely sure how to do this. I found some code and deciphered it, but it's not running correctly. What I am doing is sending two args to my script. A utility and the time-out time. I then need to grab the PID of the utility after executing it in the script. I thought a simple $! would work on getting the PID... But it doesn't. I just get the error:

kill (line number): No Such Process

As you see this doesn't help me at all. Here's the code. Please take a look.

Code:
#!/bin/bash

   #[ $# -eq 2 ] || die "cmd_timeout takes 2 arguments" 
   #command=$1
   sleep_time=$2

   # run $command in background, sleep for our timeout then kill the process if it is running
   # $! has the pid of the backgrounded job
   grep marking &
   cmd_pid=$!
   ps -ef

   # sleep for our timeout then kill the process if it is running
   (sleep $sleep_time && kill -9 $cmd_pid) &
   killer_pid=$!
   echo $killer_pid this is it

   # 'wait' for cmd_pid to complete normally.  If it does before the timeout is reached, then
   # the status will be zero.  If the killer_pid terminates it, then it will have a non-zero 
   # exit status
   wait $cmd_pid &> /dev/null
   wait_status=$?
   echo $cmd_pid

   echo $wait_status is here

   if [ $wait_status -ne 0 ]; then 
      echo "WARNING - command, $command, unclean exit" 
   else
      # Normal exit, detach and clean up the useless killer_pid
      disown $killer_pid
      kill $killer_pid &> /dev/null
   fi
As fair as I see, it looks just fine to me. No PID gets generated for the "grep" command, or it does but it doesn't show up after typing ps -ef. Then it throws that error I mentioned. Please take a look. I need this to work. Thank you!

Last edited by shuuhen; 10-18-2009 at 01:19 PM. Reason: Language
tech-it-^ is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 10-16-2009, 04:44 PM   #2 (permalink)
Registered User
 
Join Date: Aug 2006
Posts: 320
OS: XP Pro, Vista Business, Suse Linux, Win98 SE


Re: Ubuntu Bash Script PID Problems

Heres some info... When I run certain commands such as ls -R from the console and then try to look it up in ps -ef process table. It isn't there even thought I can still see it running in the other terminal. I can see the process of a grep command on the table with I run it solely in terminal but I cannot see the process run when I have the grep command embedded in a script. Whats going on?

Also I tried out the code with using vi ... It worked perfectly. So why does it work for some but not for all utilities? Thanks for the help.
tech-it-^ is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-16-2009, 07:31 PM   #3 (permalink)
Registered User
 
Join Date: Aug 2006
Posts: 320
OS: XP Pro, Vista Business, Suse Linux, Win98 SE


Re: Ubuntu Bash Script PID Problems

I think the sleep command kills the command that I give just before it gets to the sleep command. How can this be? All the help I can get would be appreciated. Thanks!
tech-it-^ is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-18-2009, 11:32 AM   #4 (permalink)
Moderator/Fedora Amb.
 
wmorri's Avatar
 
Join Date: May 2008
Location: /pm/etc
Posts: 2,805
OS: Window 7/Fedora 10

My System

Send a message via AIM to wmorri
Re: Ubuntu Bash Script PID Problems

Just to let you know it is possible that we don't know the answer to your question. I don't know if you have tried searching on google yet but that is just a thought.
__________________


Linux Forever!

wmorri is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-18-2009, 11:51 AM   #5 (permalink)
Registered User
 
Join Date: Aug 2006
Posts: 320
OS: XP Pro, Vista Business, Suse Linux, Win98 SE


Re: Ubuntu Bash Script PID Problems

yes, I've been searching for hours upon hours. I even checked with my CS prof and he said it "looks" just fine, unfortunatley, he didn't have time to diagnose the problem. I just wondered why when I run certain processes and then run a process after it, it cancels the process. If no one knows the answer, thats fine. It's just frusterating.

I found if I put "env" before the sleep command, that it works for some processes but not all. Here's a seperate question:
In my scipt I need to output the contents of the a cprogram to a untility (such as cat, find, etc...). As of now I have it as

cprogram arg1 arg2 | cat

it doesnt work like I thought it would though. if I do cat cprog arg1 arg2 it seems like it works but I thought that the pipe would take the standard output of the c program (which is to the monitor/console) and direct it to the cat command. So... Any suggestions? Thanks.
tech-it-^ is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-28-2009, 11:14 AM   #6 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 2
OS: xp


Re: Ubuntu Bash Script PID Problems

If I'm not misunderstanding your question, I think something like

ps -elf | grep $1 | grep -v "grep" | awk '{ print $4 }' | xargs kill -9

Here's how it works, step by step:

1. ps -elf : grab all the running processes
2. grep $1 : from results of step 1, pull out the line(s) that match the 1st argument to your script
3. grep -v "grep" : from the results of step 2, filter out any containing the word grep. This is to prevent this command from killing itself
4. awk '{ print $4 }' : from the results of step 3, pull out the 4th space-separated entry on the line. This should be the PID you want. IMPORTANT: you need a space between '{ and "print". Awk is fussy.
5. xargs kill -9 : xargs takes whatever it gets from stdin (which, in this case, is the output from step 4) and runs the command specified on the command line (in this case "kill -9") using the stdin stuff as arguments to that command.

Hope that helps.

Last edited by shawkins; 10-28-2009 at 11:26 AM.
shawkins is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 11-09-2009, 12:34 PM   #7 (permalink)
Registered User
 
Join Date: Aug 2006
Posts: 320
OS: XP Pro, Vista Business, Suse Linux, Win98 SE


Re: Ubuntu Bash Script PID Problems

This gives me the same problem when I tried it. I just added the env keyword in from of the sleep command and it works for the most part. I should of just did everything in C. Probably wouldn't of been as buggy. Fuzzer program works though. Thanks for the help guys.
tech-it-^ is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 05:10 PM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85