I wrote a bash script that logs keycodes in a simple file. I have put the code in a while loop because I want it to log continuosly. But if I run the bash script in the terminal, the cursor just keeps blinking suggesting that the file is indeed caught in an infinte loop. Now i want to be able to quit this loop gracefully. I don't know how. Ctrl+C won't work. I just close the terminal and force quit the process. How do i break out of the while loop as and when the user feels like quitting the program?


If you get a blinking cursor you are probably not actually in a loop. You are probably expecting input. Try Ctrl+D. If that doesn't work then open a new terminal and ps aux | grep command where command is the name of the script you wrote and then kill the pid that is returned.

  • The cursor stops blinking after some time, so i know there is a loop going on. I want this script to run on startup. I want to stop the bash script by passing a flag like bash logger.sh -stop and if i want it to start again it should be something like bash logger.sh -start How do i go about this? – Vivek Pradhan Jan 22 '13 at 7:43
  • upstart.ubuntu.com/cookbook is a good start, but I can not answer your question directly. You need to find a way to stop all your processing smoothly and use that in the stop hook. – coteyr Jan 22 '13 at 14:13

Just an idea. What if you read value from a file, and instead an infinite loop, you test the value? Something like this:

while [ "`cat mytestfile`" = 0 ]; do
    //your stuff here

And you just echo 1 > mytestfile, if you want to stop the loop.


As suggested by this answer or this more detailed one to similar questions on unix.stackexchange.com :

  • Press Ctrl+Z to stop the job, and send it to the background
  • kill %% to kill the "current" or "last stopped" job

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.