tags:

views:

61

answers:

4

I wrote the following script which enables timeout of 20 seconds if grep can not find the relevant string in the file.

The script works well, but the output from the script is like this:

./test: line 11: 30039: Killed
  1. how to disable this message from the kill command?

  2. how to tell kill command to ignore if process not exist?

THX
Yael

#!/bin/ksh  
( sleep 20 ; [[ ! -z ` ps -ef | grep "qsRw -m1" | awk '{print $2}' ` ]] && kill -9  2>/dev/null ` ps -ef | grep "qsRw -m1" | awk '{print $2}' `   ; sleep 1 ) &
RESULT=$! 
print "the proccess:"$RESULT
grep -qsRw -m1 "monitohhhhhhhr" /var
if [[ $? -ne 0 ]]
then
print "kill "$RESULT
  kill -9 $RESULT
fi
print "ENDED"


./test

the proccess:30038
./test: line 11: 30039: Killed
kill 3003
+2  A: 

kill -9 $RESULT &> /dev/null

This will send stdout and stderr to /dev/null.

tur1ng
I already try it its not work
yael
you miss the 1> or 2> and those also not workplease other advice?
yael
Alex - Aotea Studios
yael
yael
+2  A: 

you'd better look at timeout command

man timeout

NAME
       timeout - run a command with a time limit

SYNOPSIS
       timeout [OPTION] NUMBER[SUFFIX] COMMAND [ARG]...
       timeout [OPTION]

DESCRIPTION
       Start  COMMAND,  and  kill  it if still running after NUMBER seconds.  SUFFIX may be `s' for
       seconds (the default), `m' for minutes, `h' for hours or `d' for days.
zed_0xff
sorry I cant use the timeoutand I cant to install because the linux is not my system
yael
zed_0xff
yes but I did it and its not workplease see ny progarmyael
yael
you can also check solution from here http://www.unix.com/shell-programming-scripting/106960-redirecting-stderr-file-within-bash-script.html
zed_0xff
`ps -ef | grep "qsRw -m1" 2> /dev/null | awk '{print $2}' 2> /dev/null`
zed_0xff
not grep ,I need to ignore messeges from kill command not grep
yael
as I said the 2> not workI try it its not workyael
yael
not grep , I need to ignore messeges from kill command not grep
yael
you're wrong. **`kill` command does not output any messages**. you can check this by running one long command in one terminal, and `kill`-ing it from a second terminal. you'll see 'killed' message on first terminal. `kill` not outputs anything.
zed_0xff
+1  A: 

Messages are printed by your shell, not by the killed process.

Try runnning the proccess to be killed in an another shell, encapsulating command being killed like this:

sh -c 'command_to_be_inettrupted&'

The idea is to make the shell instance exit earlier than the process it started. You may also need to "nohup" your command, but that was unnecessary on my system.

For example:

sh -c 'sleep 10&' ; sleep 1; killall sleep

This code won't produce any output, despite first sleep instance being killed.

Basilevs
yael
That doesn't matter. Just put everything in another shell.You might also remove parenthesis.
Basilevs
I do that and its not work between (.......)what to do next????????
yael
I also do that and its not work????????????????????su - c 'sleep 20 ; [[ ! -z ` ps -ef | grep "qsRw -m1" | awk '{print $2}' ` ]]
yael
You've misunderstood my idea. Code you've just shown is the interrupting one. I propose to encapsulate the one being interrupted.A part where qsRw program is started.
Basilevs
please give exampleanyway awk cant work under su -c .....
yael
su -c 'qsRw -m1'; sleep 1;killall qsRw
Basilevs
A: 

I think this message comes from job control. Try turning it off with set +m
if that doesn't work under ksh, try the script with #!/bin/bash

frankc