Display the number of running rule threads

Hello,

i’ m looking for a way to display the number of running rule threads.
Someone knows a simple way or is this binding the only solution?

Thanks for our help.

display where and how?

The best approach would be an item. So it would be possible to persist the threads and investigate later. But each idea is welcome.

I need this during the development from rules.
I have rules to control rollershutters. For each shutter exist a shadow shutter (hashmap) to store the current position. The shutter are controllable by UI in predefined steps, up/down/stop and with wall plugs.
To detect control with wall plugs is it nessesary to monitoring relay contacts. These contacts are in a group and the members are triggering a rule.
Then at the same time many shutter drive this rule is triggered frequently. But each trigger is nessesary to calculate the correct position.

Sometimes works everything well and sometimes the shadow modell is broken.

I mean the reason is the limited thread pool for running rules but the best is to see the count of threads and not to guess.

In that case i know if no other way. All the other ways involve running something from the command line or the karaf console.

Unless your have more than five of these rules routinely running at the same time and the rules take an extended amount of time to complete ( e.g. using Thread::sleep or use long running calls) this is unlikely to be your problem. If you do, then you need to focus on making your rules run as quickly as possible by using timers instead of sleeps and moving the calls to long running actions outside of the critical path of these rules.

Thanks rich for your answer.
Can you please explain the way outside the rule. It’s only for developing so this is completely OK.

I must check the idea with the rule threads. The error is really tricky.

I’m using the lcn binding and I guess that sometimes that a trigger in the query is lost. In the moment I have increased the maximum rule threads and it works fine. But I remember me you called it aspirin fix. :thinking:

If you log into the karaf console you can run threads and it will give you the current status of all the running threads in karaf. You will probably want to filter them down to just the Rules threads.

You can also see what threads are being used using top. See

Hey Rich,

Can you help me with this syntax?

Both of the commands return the number of waiting threads but it returns the number twice. Is there anyway to just return the number once?

shell:threads | grep TIMED_WAITING | wc -l
shell:threads | grep TIMED_WAITING | grep -v \n\s | wc -l

openhab> shell:threads | grep TIMED_WAITING | grep -v \n\s | wc -l
86 86 ← only want one number to be displayed vs. two of them

Best, Jay

Try this:

shell:threads | grep TIMED_WAITING | wc -l | head -1

For an output like that, in the same line, you could do this to get the first before the space:

shell:threads | grep TIMED_WAITING | grep -v \n\s | wc -l | cut -d ' ' -f 1
1 Like

On the Karaf console, CUT isn’t valid.

shell:threads | grep TIMED_WAITING | grep -v \n\s | wc -l | cut -d ’ ’ -f 1
Command not found: cut

On the Karaf console, the head option -1 isn’t valid.

openhab> shell:threads | grep TIMED_WAITING | wc -l | head -1
head: invalid option ‘1’

Best, Jay

Here’s the solution that works with OH 2.x

rule "Check Karaf Waiting Thread Counts"
	when
	    Time cron "0 17 2,5,8,10,12,14,16,18,20 * * ?" or		// At second :00 at minute :17 2,5,8,10 am 12,2,4,6,8 pm every day
		Item KarafThreadsRerun changed to ON
	then

		if (systemStarted.state != ON && gInternet.state == ON && KarafThreadStart_tAlive === null) {

		    logInfo("KarafThreads", "Karaf Waiting Thread Count Executing.")

			KarafThreadStart_tAlive = createTimer(now.plusMillis(1000), [ |
				logInfo("tAlive" , "KarafThreadStart_tAlive timer ran out -> cancelling timer.")
			
	    		var String results = executeCommandLine("/usr/bin/bash /var/services/homes/openhab/ThreadCount.sh", 10000)	
				KarafThreadsResult.postUpdate(results) 
		        logInfo("KarafThreads", "Karaf Waiting Thread Count as a String is " + results)

				try {
						var Number Numberresults = (Float::parseFloat(String::format("%s",results).replace(',','.')))		//  Format a string to a number
		
				        logInfo("KarafThreads", "Karaf Waiting Thread Count as a Number is " + Numberresults)
		
						if (systemStarted.state != ON && gInternet.state == ON && gPort25.state == ON && Numberresults >= 100.0) {
		
							val String subjectemailkaraftest = "openHAB - Karaf Threads"	
							val String bodyemailkaraftest	 = "openHAB - Karaf Waiting Thread Count as a Number is " + Numberresults +"." 
							sendMail(JaygMail, subjectemailkaraftest, bodyemailkaraftest)
						}
		
				    } catch (Exception e) {

				        logInfo("KarafThreads", "Karaf Waiting Thread Count is NOT a number.")
				    }

			    KarafThreadsRerun.postUpdate(OFF)

    			KarafThreadStart_tAlive.cancel()
					Thread::sleep(1500)  
				KarafThreadStart_tAlive = null
			])
		}
end

Script on the file system:

#!/bin/bash
/volume1/@appstore/openHAB/runtime/bin/client shell:threads | grep TIMED_WAITING | wc -l | grep [0-9]$1
exit

Best, Jay