0

I have a shell file which I execute then, at the end, I get the possibility to press ENTER and run it again. The problem is that each time I press ENTER a new process is created and after 20 or 30 rounds I get 30 PIDs that will finally mess up my Linux. So, my question is: how can I make the script run always in the same process, instead of creating a new one each time I press ENTER?

Code:

#!/bin/bash

echo "Doing my stuff here!"

# Show message
read -sp "Press ENTER to re-start"
# Clear screen
reset
# Re-execute the script
./run_this.sh

exec $SHELL
sondra.kinsey
  • 583
  • 7
  • 18
ali
  • 10,927
  • 20
  • 89
  • 138
  • 1
    You should use some sort of looping construct rather than a recursive execution. If your test can be at the beginning, a simple while loop will do, if it needs to be at the end you might read http://stackoverflow.com/questions/16489809/emulating-do-while-loop-in-bash – Chris Stratton Apr 17 '14 at 17:37

1 Answers1

1

You would need to exec the script itself, like so

#!/bin/bash

echo "Doing my stuff here!"

# Show message
read -sp "Press ENTER to re-start"
# Clear screen
reset
# Re-execute the script
exec bash ./run_this.sh

exec does not work with shell scripts, so you need to use execute bash instead with your script as an argument.

That said, an in-script loop is a better way to go.

while :; do
  echo "Doing my stuff here!"

  # Show message
  read -sp "Press ENTER to re-start"
  # Clear screen
  reset
done
chepner
  • 497,756
  • 71
  • 530
  • 681