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"
Tomas
  • 514
  • 4
  • 13
  • 37

6 Answers6

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"
drkunibar
  • 1,327
  • 1
  • 7
  • 7
  • +1 This solves the immediate issue in the shell script. Using `timeout` is arguably a better option. – Jonathan Leffler Apr 01 '14 at 05:56
  • @drkunibar script not working,script runs command after 5 seconds – Tomas Apr 01 '14 at 06:42
  • 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 Apr 01 '14 at 07:00
  • @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 Apr 01 '14 at 07:16
  • @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` – drkunibar Apr 01 '14 at 07:23
  • @drkunibar yes `$pid` before and after the `sleep` are same.script stops command after 5 second and then print message and then resume the command again. – Tomas Apr 01 '14 at 10:09
  • What is `wrun`? It seems to me, that `wrun` calls the commands insides the quotes. Have you tried `kill -9`? Can you check, that the process ID of `wrun` after the `kill` is the same than before? Maybe a service triggers `wrun` again if yout kill it. – drkunibar Apr 01 '14 at 10:22
  • @drkunibar if we use only `kill` command and skip `sleep` command the script kill the command `(/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat')` successfully if we use `sleep` then it resume command `(/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat')` after `kill` command – Tomas Apr 01 '14 at 11:04
  • Are your shure you wrote `kill $pid` and not your old command `kill $!`? – drkunibar Apr 01 '14 at 11:24
  • @drkunibar please help in this question http://stackoverflow.com/questions/22807051/can-we-use-php-in-bash-script/22807270?noredirect=1#22807270 – Tomas Apr 02 '14 at 09:37
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'
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
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'
slm
  • 15,396
  • 12
  • 109
  • 124
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"
Rustam
  • 182
  • 1
  • 9
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"
chaos
  • 8,162
  • 3
  • 33
  • 40
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"
Laurent C.
  • 196
  • 1
  • 9