spawn.sh

本文介绍了一个使用Bash脚本实现无限循环并管理自身进程ID(PID)的方法。通过解析脚本如何创建并记录多个实例的PID,展示了如何统计当前运行的脚本实例数量。同时提醒读者注意资源消耗,避免长时间运行导致系统资源被过度占用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/bash
# spawn.sh
PIDS=$(pidof sh $0) # Process IDs of the various instances of this script.#打印出该脚本的pid进程
P_array=( $PIDS ) # Put them in an array (why?). #引入变量
echo $PIDS # Show process IDs of parent and child processes #echo 进程数.
let "instances = ${#P_array[*]} - 1" # Count elements, less 1.#总数减1 获得进程总数。
# Why subtract 1?
echo "$instances instance(s) of this script running."
echo "[Hit Ctl-C to exit.]"; echo
sleep 1 # Wait.
sh $0 # Play it again, Sam.
exit 0 # Not necessary; script will never get to here.
# Why not?
# After exiting with a Ctl-C,
#+ do all the spawned instances of the script die?
# If so, why?
# Note:
# ----
# Be careful not to run this script too long.
# It will eventually eat up too many system resources.
# Is having a script spawn multiple instances of itself
#+ an advisable scripting technique.
# Why or why not?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值