Shell脚本中的杀死进程 [英] Killing process in Shell Script

查看:609
本文介绍了Shell脚本中的杀死进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题:运行shell脚本时,我启动了一个程序 在无限循环中运行.过了一会儿我想停下来然后我可以 再次使用不同的参数.现在的问题是我如何找出 该程序在我执行的时候?基本上,我想做这样的事情:

I have a very simple problem: When I run a shell script I start a program which runs in an infinite loop. After a while I wanna stop then this program before I can it again with different parameters. The question is now how do I find out the pid of the program when I execute it? Basically, I wanna do something like that:

echo "Executing app1 with param1"  
./app1 param1 &  
echo "Executing app1"  
..do some other stuff  
#kill somehow app1
echo "Execution of app1 finished!"

谢谢!

推荐答案

在大多数shell(包括Bourne和C)中,您在后台启动的最后一个子进程的PID将存储在特殊变量$!中. >

In most shells (including Bourne and C), the PID of the last subprocess you launched in the background will be stored in the special variable $!.

#!/bin/bash
./app1 &
PID=$!
# ...
kill $PID

特殊变量"部分下有此处的一些信息.

这篇关于Shell脚本中的杀死进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆