#!/bin/bash
# spawn.sh
# spawn.sh
PIDS=$(pidof sh $0) # Process IDs of the various instances of this script.
P_array=( $PIDS ) # Put them in an array (why?).
echo $PIDS # Show process IDs of parent and child processes.
let "instances = ${#P_array[*]} - 1" # Count elements, less 1. Why subtract 1?
echo "$instances instance(s) of this script running."
echo "[Hit Ctl-C to exit.]"; echo
P_array=( $PIDS ) # Put them in an array (why?).
echo $PIDS # Show process IDs of parent and child processes.
let "instances = ${#P_array[*]} - 1" # Count elements, less 1. Why subtract 1?
echo "$instances instance(s) of this script running."
echo "[Hit Ctl-C to exit.]"; echo
sleep 10 # Wait.
sh $0 # Play it again, Sam.
sh $0 # Play it again, Sam.
exit 0 # Not necessary; script will never get to here. Why not?
几个要点:
1.pidof的用法
以前结束一个进程:ps -ef | grep xxx 返回一个pid,然后,kill pid.
也可以还有一个命令:pidof xxx
pidof xxx 返回一个pid, 那么用pidof xxx | xargs kill 是一个更有效率的方法!
pidof xxx 返回一个pid, 那么用pidof xxx | xargs kill 是一个更有效率的方法!
2.shell 数组的语法
注意用法,已学习备忘.
转载于:https://blog.51cto.com/mrdba/406146