-1

I have this command in my shell script that runs forever- it wouldn't finish unless I do ctrl-c. I have been trying to look up how to send ctrl-c signal to script and all the answers have been some sort of kill $! or kill$$ or such. My problem is that the command never finishes, so it never goes on to the next command like my "kill" commands or anything else. I have to manually hit the ctrl-C in my terminal for it to even execute kill $!. I'm sure there is a way to work around this but I am not sure what. Thanks in advance!

hyeuc
  • 47
  • 3
  • [How to stop infinite loop in bash script gracefully?](https://stackoverflow.com/q/11203201/608639), [How to stop the loop bash script in terminal?](https://unix.stackexchange.com/q/48425/56041), [Terminating an infinite loop](https://unix.stackexchange.com/q/42287/56041), [How to force quit out of an infinite loop in a bash script gracefully](https://askubuntu.com/q/245812), etc. – jww Oct 11 '18 at 03:30

1 Answers1

1

There are several approaches to this problem. The simplest (but not most robust) is (perhaps) to simply run your long running command in the background:

#!/bin/sh

long-running-command &   # run in the background
sleep 5  # sleep for a bit
kill %1  # send SIGTERM to the command if it's still running
William Pursell
  • 204,365
  • 48
  • 270
  • 300