![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| 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: * 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 |
|
|||||||
| Linux Support Linux - Operating Systems and Applications Support |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#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
Last edited by shuuhen; 10-18-2009 at 01:19 PM. Reason: Language |
|
|
|
| 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 |
|
|
#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. |
|
|
|
|
|
#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!
|
|
|
|
|
|
#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. |
|
|
|
|
|
#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. |
|
|
|
|
|
#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.
|
|
|
|
![]() |
| Thread Tools | |
|
|