ex_directory
Posts: 30
Joined: Thu May 29, 2014 9:08 pm
Location: Cambridge, UK

SOLVED - Starting java in rc.local consuming high cpu

Sun Oct 16, 2016 6:12 pm

Hi, I have a bash script to start a java app (server type app)

If i start this in rc.local, i put...

Code: Select all

/home/pi/house/start.sh &
Then in start.sh i have

Code: Select all

#!/bin/bash
cd /home/pi/house
sudo -u pi java -cp "*" projects.house.Main
Then i log in with putty and using top i can see java running at >90%

If i kill this and run the same command in putty like this...
java -cp "*" projects.house.Main

it uses 0.7% cpu!

I guess there is something obvious i am doing wrong? Not a pi export but know my java and java app is fine, but i have cobbled the scriptiing side together from googling.
Last edited by ex_directory on Tue Oct 18, 2016 10:48 pm, edited 1 time in total.

ex_directory
Posts: 30
Joined: Thu May 29, 2014 9:08 pm
Location: Cambridge, UK

Re: Starting java in rc.local consuming high cpu

Tue Oct 18, 2016 4:36 pm

Bump - anyone?

mutley
Posts: 61
Joined: Sat Jan 02, 2016 8:06 pm

Re: Starting java in rc.local consuming high cpu

Tue Oct 18, 2016 6:32 pm

Since it looks like your java program is a daemon, you should really write a sysvinit or systemd script to start it, and not run it in the background from rc.local. rc.local is really for running something 1 time on startup that terminates, not for a process that runs in the background.

But in saying that, I don't actually see anything wrong with your script. Assuming "sudo /home/pi/house/start.sh &" doesn't take >90% cpu all I can think is your java app is starting before some system dependency it has, has been started. And the application is stuck in some repetitive mode, OR your java app doesn't like running without a terminal.

So first try using nohup:-

nohup /home/pi/house/start.sh &
- or -
sudo nohup -u pi java -cp "*" projects.house.Main
- or -
I seem to remember issues with sudo and nohup, so use the -b flaf in sudo.
sudo -b -u pi java -cp "*" projects.house.Main

You can also try screen rather than nohup. screen will mimic a terminal and allow you to connect and disconnect to the terminal.

If none of that works, OR you want to do it the correct way. Trash those scripts and write a sysvinit or systemd script.
Here is sysvinit script to daemonize a java program. (not the best example for a pi IMO, but it should work with some modification)
http://www.jcgonzalez.com/linux-java-se ... er-example

Here is systemd. This may be your better option as Raspbian is moving away from sysvinit to systemd but I believe currently supports both. (although I have not tried systemd yet, so can't confirm).
http://ae.koroglu.org/alternative-way-t ... tos7rhel7/

ex_directory
Posts: 30
Joined: Thu May 29, 2014 9:08 pm
Location: Cambridge, UK

Re: Starting java in rc.local consuming high cpu

Tue Oct 18, 2016 10:47 pm

mutley wrote:Since it looks like your java program is a daemon, you should really write a sysvinit or ...
Thanks very much, your reply got me thinking.

The app can run as a service but also supports interaction via a console.

The java app has a daemon thread that runs to allow console input, but wrapping this is another thread waiting for user input.

It seems when started through bash, this is then consuming high cpu load, but when started say through a putty terminal, it was fine with 0% cpu load.

To confirm this i used "top" to get process id then "top -p<pid> -h" to view the threads and the offending thread that was high cpu, then "jstack <pid>" to check what that thread was (probably should have done this first!), it confirmed it was the console input threads that were consuming the high load when started through bash.

Not being a linux expert, but suspect your suggestions may also have solved the problem, anyway, i decided it was quicker to modify my java to add an startup parameter that it was going to run as a service with no console and this is now working like a dream! 0.5% cpu load which I suspect without checking will be the JVM and garbage collector threads.

Thanks.

Return to “General discussion”