Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何提供ctrl+;shell脚本中的C?_Bash_Shell - Fatal编程技术网

Bash 如何提供ctrl+;shell脚本中的C?

Bash 如何提供ctrl+;shell脚本中的C?,bash,shell,Bash,Shell,下面是我试图执行/自动化测试的脚本 while [ 1 ]; do val=`expr $val + 1` ksh ./run.ksh //This line needs the keyboard interaction so i can't run in background. It takes too long time to complete so i need to kill the above command using ctrl+C echo "pid=$!" ech

下面是我试图执行/自动化测试的脚本

 while [ 1 ]; do
 val=`expr $val + 1`
 ksh ./run.ksh   //This line needs the keyboard interaction so i can't run in background. It takes too long time to complete so i need to kill the above command using ctrl+C 
 echo "pid=$!"
 echo "pid=$$"
 sleep 40
 val1=val;
 done;
./run.ksh-是一个脚本,它具有一些业务逻辑,可以将数据发送到其他计算机并等待响应。即使收到响应,它也会等待合理的时间来完成处理。因为它等待连接关闭并执行其他清理活动

我的问题是,我想在几秒钟后通过发送ctrl+C来终止该脚本。当我在谷歌上搜索时,我发现了$!可以用于获取后台进程的进程id,但在这种情况下不能使用相同的id

是否可以在shell脚本中发送ctrl+C


提前感谢。

使用
timeout
命令。例如,它将在30秒后被杀死

timeout 30 ksh ./run.ksh

使用:
kill-SIGINT pid
您需要
pidof sleep
。请记住调用sleep,为
sleep
生成一个子shell,它的
PID
与脚本
PID
是分开的。由于您混合了
bash
ksh
,请注意
sleep
bash
中使用时是一个外部程序,但在
ksh
中是一个shell内置程序(因此它不会创建子shell)。我需要删除“ksh./run.ksh”,正如我前面提到的,它需要键盘交互,我不能在后台运行。有没有办法从主脚本中删除ksh./run.ksh命令。我不想与睡眠有任何关系,因为我正在使用它。感谢您的输入。脚本run.ksh没有向服务器发送任何数据。我想补充的是,它是一个Seag将Diameter SH消息发送到服务器的ull脚本。