我遇到命令Jobs的问题,因为正在返回1个正在执行的作业 当所有启动的线程(在后台)已经中止时。
要知道正在运行的作业,我正在使用命令:
工作| wc -l </ p>
结果: 2
当我只有一个线程(在后台运行)时,该命令返回2而不是一个。
知道该命令为什么返回2个正在运行的作业?
我真的不明白这种行为,这会导致我的korn shell脚本出现问题。
_check_rfs_thread()
{
(...)
if [ $NBR_OF_JOBS -eq 0 ] then
# If all threads abort then should exit program here...
# This is the problem... because a "dummy" jobs keeps "holding on"
echo "No available threads in 'ready for start' status for process "$PROGRAM_NAME"."
echo "Check restart_program_status table for process "$PROGRAM_NAME"."
return 1
fi
}
(...)
_launch_thread() {
# This function launchs threads in background with &
}
(...)
SLOTS=15
while [ $LAUNCH_THREAD -eq 0 ]
do
NBR_OF_JOBS=`jobs | wc -l`
echo '1 NBR_OF_JOBS: ['$NBR_OF_JOBS']'
while [[ $NBR_OF_JOBS -ge $SLOTS ]] # When all threads are available
do
sleep 1
NBR_OF_JOBS=`jobs | wc -l`
echo '2 NBR_OF_JOBS: ['$NBR_OF_JOBS']'
done
echo '3 NBR_OF_JOBS: ['$NBR_OF_JOBS']'
_check_rfs_thread $PROGRAM_NAME $SLOTS $NBR_OF_JOBS
_launch_thread()
done
当我的所有线程都被中止时,Jobs返回的命令总是执行1个作业。 因此,“if [$ NBR_OF_JOBS -eq 0]”始终为false([1 -eq 0])且程序继续执行
- Execution 1 - didint launch any thread:
1 NBR_OF_JOBS: [1]
3 NBR_OF_JOBS: [1]
- Execution 2 - after launch a thread
1 NBR_OF_JOBS: [2]
3 NBR_OF_JOBS: [2]
- Execution 3 - a thread gets aborted
1 NBR_OF_JOBS: [2]
3 NBR_OF_JOBS: [2]
- Execution 4 - no threads to launch and no threads running in
background
1 NBR_OF_JOBS: [1]
3 NBR_OF_JOBS: [1]