簡體   English   中英

重新啟動Shell腳本而不在Linux中創建新進程

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

我有一個要執行的外殼文件,最后,我可以按Enter鍵並再次運行它。 問題是,每當我按ENTER鍵時,都會創建一個新進程,經過20或30回合后,我會得到30個PID,這些PID最終會弄亂我的Linux。 因此,我的問題是:如何使腳本始終在同一進程中運行,而不是每次按ENTER都創建一個新腳本?

碼:

#!/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

您將需要exec腳本本身,就像這樣

#!/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不適用於shell腳本,因此您需要使用execute bash來代替腳本。

也就是說,腳本內循環是一種更好的方法。

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

  # Show message
  read -sp "Press ENTER to re-start"
  # Clear screen
  reset
done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM