1

Hi I have used the following script, which after it has run and exited, causes my command line to display no text when typing, and after each carriage return instead of starting a new line like

root@alix:~# 

root@alix:~# 

I get

root@alix:~# root@alix:~# root@alix:~# root@alix:~#  etc

When using ctrl c I get

root@alix:~# 
            root@alix:~# 
                        root@alix:~#  etc

Here is the script:

#!/bin/bash

###
### Run command for given number of seconds then kill it
###

read -p "How long should I run for? ==> " count_secs
echo "Time specified: " $count_secs

if [ $count_secs -gt 0 ]
then
   ###
   ### number of seconds greater than zero
   ###

   watch -n 0.5 'iw dev wlan0 station dump | grep "signal avg" >> processmonitor.log' >>/dev/null & 

   ###
   ### assume that the PID of the command is $$
   ###
   my_PID=$!
   sleep $count_secs
   kill -15 $my_PID
fi

Thanks.

2
  • I run this script, but nothing from what you say didn't happend at me... Apr 19, 2013 at 13:07
  • Anyway, if you want that your script to do what you proposed, i suggest you to use 1 instead 0.5 like argument for watch Apr 19, 2013 at 13:21

1 Answer 1

0

You can use the following code without the need to kill any process, after the end of the specified time (secs) it will exit.

#!/bin/bash
###
### Run command for given number of seconds then kill it
###
read -p "How long should I run for? ==> " count_secs
echo "Time specified: " $count_secs
# $SECONDS is a shell variable
# we can use it in conjuction with
# our user input 
while [ "$SECONDS" -le "$count_secs" ]
do
  # execute any commands here (put your code bellow and remove echo commands)
  echo `date +%r`
  echo "i am doing my work now"
  # sleep (in secs) before the next execution
  sleep 1
done
# when the count_secs elapses exit
exit 0
3
  • With echo $(cmd), the echo and command substitution is pointless. Should just run cmd instead; no point in capturing the output only to output it again.
    – geirha
    Apr 19, 2013 at 14:40
  • Both echo commands iside the loop are for demonstration purposes and will be replaced by the actual job as mentioned in code comments.
    – Stef K
    Apr 19, 2013 at 14:46
  • Thanks Stef, I ended implementing something quite similar. I am still not sure why I was getting those weird side effects.
    – Radagasp
    Apr 22, 2013 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.