重新启动Shell脚本而不在Linux中创建新进程

重新启动Shell脚本而不在Linux中创建新进程

本文介绍了重新启动Shell脚本而不在Linux中创建新进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要执行的shell文件,最后,我可以按并再次运行它.问题是,每次按都会创建一个新进程,经过20或30回合后,我会得到30个PID,这些PID最终将使我的Linux混乱.因此,我的问题是:如何使脚本始终在同一进程中运行,而不是每次按时都创建一个新脚本?

I have a shell file which I execute then, at the end, I get the possibility to press and run it again. The problem is that each time I press 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 ?

代码:

#!/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脚本本身,就像这样

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不适用于shell脚本,因此您需要使用execute bash来代替脚本作为参数.

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

这篇关于重新启动Shell脚本而不在Linux中创建新进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:52