Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
I run this script, but nothing from what you say didn't happend at me... –  Radu Rădeanu Apr 19 '13 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 –  Radu Rădeanu Apr 19 '13 at 13:21
add comment

1 Answer

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
share|improve this answer
    
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 '13 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 '13 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 '13 at 19:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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