以进程号为标志的临时文件。
Apache共计有:1个主进程、8个子进程
ps -e:可以获取到所有程序的pid
Apache的进程号如下:
[root@mail mnt]# ps -e |grep httpd
8256 ? 00:00:00 httpd
8258 ? 00:00:00 httpd
8259 ? 00:00:00 httpd
8260 ? 00:00:00 httpd
8261 ? 00:00:00 httpd
8262 ? 00:00:00 httpd
8263 ? 00:00:00 httpd
8264 ? 00:00:00 httpd
8265 ? 00:00:00 httpd
创建临时文件脚本,脚本共分为6部分
1、获取到所有httpd应用程序的进程号
2、把进程号存入一个临时文件中
3、从临时文件中取出所有的apache的进程号
4、使用for循环用kill杀掉所有的httpd进程
5、删除之前生成的临时文件
6、输出关闭进程后的消息
代码如下:
#!/bin/bash
#createtmp.sh
tmpfile=$$.txt
ps -e|grep httpd|awk '{print $1}' >>$tmpfile
for pid in `cat $tmpfile`
do
echo "apache ${pid} is killed!!!"
done
sleep 1
rm -rf $tmpfile
echo "apache is closed!!!"
其中:$$代表当前程序的pid
运行结果如下:
[root@mail mnt]# ps -e |grep httpd
8256 ? 00:00:00 httpd
8258 ? 00:00:00 httpd
8259 ? 00:00:00 httpd
8260 ? 00:00:00 httpd
8261 ? 00:00:00 httpd
8262 ? 00:00:00 httpd
8263 ? 00:00:00 httpd
8264 ? 00:00:00 httpd
8265 ? 00:00:00 httpd
[root@mail mnt]# ls
createlog.sh createtmp.sh
[root@mail mnt]# ./createtmp.sh
apache 8256 is killed!!!
apache 8258 is killed!!!
apache 8259 is killed!!!
apache 8260 is killed!!!
apache 8261 is killed!!!
apache 8262 is killed!!!
apache 8263 is killed!!!
apache 8264 is killed!!!
apache 8265 is killed!!!
apache is closed!!!
[root@mail mnt]# ls
createlog.sh createtmp.sh
[root@mail mnt]# ps -e|grep httpd
注:脚本执行后,临时文件已被删除,Apache的进程被彻底kill。