Re-start shell script without creating a new process in Linux

余生颓废 提交于 2019-12-12 01:33:00

问题


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

回答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


来源:https://stackoverflow.com/questions/23139555/re-start-shell-script-without-creating-a-new-process-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!