0

I have a bash script abcd.sh,in which I want to kill this command(/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat') after 5 sec but in this script it kill sleep command after 5 second.

#!/bin/sh
/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat' &
sleep 5
kill $! 2>/dev/null && echo "Killed command on time out"
0

6 Answers 6

6

Try

#!/bin/sh
/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat' &
pid=$!
sleep 5
kill $pid 2>/dev/null && echo "Killed command on time out"

UPDATE:

A working example (no special commands)

#!/bin/sh
set +x
ping -i 1 google.de &
pid=$!
echo $pid
sleep 5
echo $pid
kill $pid 2>/dev/null && echo "Killed command on time out"
Sign up to request clarification or add additional context in comments.

10 Comments

+1 This solves the immediate issue in the shell script. Using timeout is arguably a better option.
@drkunibar script not working,script runs command after 5 seconds
Sorry, but I can't reproduce your problem. What command starts after 5 seconds? I have updated my answer with an example that should work on every system (changed your command with a ping)
@drkunibar /usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat' resume its working.After 5 sec it shows the echo message and resume its working.
@Tomas Can your echo pid like it is in the example that we can see if kill calls the right pid. Maybe you need to call kill -9 $pid
|
6

You should use instead the timeout(1) command:

timeout 5 /usr/local/bin/wrun \
      'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat'

2 Comments

+1, this is the correct answer for systems that ship with timeout. Sadly, timeout was only introduced in coreutils 7 which means it's not available on e.g. CentOS 5.
neither on ubuntu 10.04
2

Rather then try and construct your own mechanism why not make use of the timeout command.

Example

$ date; timeout 5 sleep 100; date
Tue Apr  1 03:19:56 EDT 2014
Tue Apr  1 03:20:01 EDT 2014

In the above you can see that timeout has terminated the sleep 100 after only 5 seconds (aka. the duration).

Your example

$ timeout 5 /usr/local/bin/wrun \
    'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat'

Comments

1

Try this:

#!/bin/sh
/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat' &
sleep 5
pkill "wrun" && echo "Killed command on time out"

Comments

0

This is because the variable $! contains the PID of the most recent background command. And this background command is in your case sleep 5. This should work:

#!/bin/sh
/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat' &
PID=$!
sleep 5
kill $PID 2>/dev/null && echo "Killed command on time out"

3 Comments

Right, background command. Sleep is not being executed in the background.
@chaos script not working.script runs command after 5 seconds
What is /usr/local/bin/wrun? Is it this gist.github.com/orgoj/5326037 ?
0

You could use something like:

#!/bin/sh
/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat' &
PID=`ps -ef | grep /usr/local/bin/wrun | awk '{print $1}'`
sleep 5
kill $PID 2>/dev/null && echo "Killed command on time out"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.