Keeping my JAVA (jar) program running on Heroku - java

I have made a bot in Java and it works great when I run the command:
heroku run nohup java -jar bot.jar
However, after 24h Heroku closes it because of their Dyno policy. So, I thought of making a Procfile in order for it to execute the command above everytime it comes alive again.
I've tried placing the following commands inside the Procfile:
worker: java -jar bot.jar
worker: nohup java -jar bot.jar
And I even made a run.sh file that contains:
#!/bin/bash
nohup java -jar bot.jar
In order to make a Procfile that has:
worker: sh run.sh
Sadly, none of this works, because everytime the Dyno executes the Procfile (right after I push a new version of it through git), the bot never comes alive. The logs show:
2017-06-07T13:17:33.651806+00:00 app[api]: Starting process with command `sh run.sh` by XXXX
2017-06-07T13:17:37.958327+00:00 heroku[run.2087]: State changed from starting to up
2017-06-07T13:17:37.948696+00:00 heroku[run.2087]: Awaiting client
2017-06-07T13:17:38.283581+00:00 heroku[run.2087]: Starting process with command `sh run.sh`
2017-06-07T13:17:52.807189+00:00 heroku[run.2087]: Client connection closed. Sending SIGHUP to all processes
2017-06-07T13:17:53.343009+00:00 heroku[run.2087]: Process exited with status 129
2017-06-07T13:17:53.359331+00:00 heroku[run.2087]: State changed from up to complete
2017-06-07T13:24:32.000000+00:00 app[api]: Build started by user XXXX
2017-06-07T13:24:43.090567+00:00 app[api]: Deploy YYYY by user XXXX
2017-06-07T13:24:43.090567+00:00 app[api]: Release v17 created by user XXXX
2017-06-07T13:24:32.000000+00:00 app[api]: Build succeeded
I have no idea what to do in order for my bot to run from the Procfile.
Can you help me?
Thanks.

Try running:
$ heroku ps:scale worker=1
This will ensure that one worker process is always running.

Related

Deploying Gradle project on Heroku

The problem is when I have the project deployed on Heroku after running
heroku local web
it would show the following
C:\Users\Richer\OneDrive\Project>heroku local web
5:22:24 PM web.1 | Error: Could not find or load main class $JAVA_OPTS
[DONE] Killing all processes with signal SIGINT
5:22:25 PM web.1 Exited with exit code null
and if I run
heroku ps
the shell will show
C:\Users\Richer\OneDrive\Project>heroku ps
Free dyno hours quota remaining this month: 549h 21m (99%)
Free dyno usage for this app: 0h 0m (0%)
For more information on dyno sleeping and how to upgrade, see:https://devcenter.heroku.com/articles/dyno-sleeping
=== web (Free): java -Dserver.port=$PORT $JAVA_OPTS -jar build/libs/*.jar (1)
web.1: crashed 2018/10/12 17:22:22 +0300 (~ 14m ago)
my Procfile looks like this
web: java -Dserver.port=$PORT $JAVA_OPTS -jar build/libs/*.jar
If it helps I have two java classes at src/main/java folder as it should be and after many attempts to upload and tun the project I've also built a jar file and its location is at out/artifacts/1_jar/1.jar though this didn't help.
So what do I do?
In addition, to successfully build a project I had to put this 3 strings in the build.gradle as it was described here
ask stage(dependsOn: ['build', 'clean'])
build.mustRunAfter clean
heroku config:set GRADLE_TASK="build"
On Windows, environment variables are used with the $ notation. You'll need to create a second file called Procfile.windows with the contents:
web: java -Dserver.port=%PORT% %JAVA_OPTS% -jar build\libs\*.jar
Then run heroku local web -f Procfile.windows
But leave the $ for Heroku, which uses Linux.

Running Executable jar file keep hanging on Jenkins

I am very newbie in Jenkins, I've created Jenkins job where it will execute somekind like this jar command in Jenkins job:
ECHO **********************************
ECHO Upgrade Devices
ECHO **********************************
cd c:\JenkinsPull
java -jar DevicesUpgrade.jar PC1 %ProjectName%
When I start to build the Jenkins job it able to run the DevicesUpgrade.jar file until last part of jar process before it finish, sudddenly it just stuck there till I abort the job.
So I think there must be a bug inside the jar file, but it is weird because after that, I tried to run java -jar DevicesUpgrade.jar PC1 %ProjectName% on command prompt, it didn't stuck at all and it able to finish up the jar.
Is there anything that I need to do on Jenkins to fix this hang problem?
Here is the output I got from Jenkins:
c:\JenkinsPull>java -jar DevicesUpgrade.jar PC1 lex_l11_gms
%%%%f95eac91%%%%%
lexl11g_64-ota-L11_D01.00.48_userdebug.zip
1517844524458
%%%%%%%%%%%%%%%%%%%%%%%%
adb -s shell f95eac91 "getprop | grep ro.build.version.incremental"
\\ANDROIDTEST-05\ARM_png\JenkinsPull\BSP\lex_l11_gms\lexl11g_64-ota-
L11_D01.00.48_userdebug.zip
1.143848721E9
[*--------------------------------------------------------------------------
--------------------------]
Here it keep loading the jenkins job till I need to abort the Jenkins job.
Here is the output I got when I am running the jar via Command Prompt:
c:\JenkinsPull>java -jar DevicesUpgrade.jar PC1 lex_l11_gms
%%%%f95eac91%%%%%
lexl11g_64-ota-L11_D01.00.48_userdebug.zip
1517844524458
%%%%%%%%%%%%%%%%%%%%%%%%
adb -s shell f95eac91 "getprop | grep ro.build.version.incremental"
\\ANDROIDTEST-05\ARM_png\JenkinsPull\BSP\lex_l11_gms\lexl11g_64-ota-
L11_D01.00.48_userdebug.zip
1.143848721E9
[*--------------------------------------------------------------------------
--------------------------]
Upgrade Device Completed..!!
c:\JenkinsPull>

Ansible terminates my app with "nohup"

I have a task in Ansible playbook that executes a .jar file in the background, but after finishing that task, the (java) app terminates.
- name: Run Java app in the background
shell: nohup java -jar app.jar &
I need the app running for tasks further down in the playbook. Any ideas??
NOTE: When I run it in Putty ssh session it runs smoothly and the app stays in background.
The most likely reason is attached IO. Try:
- name: Run Java app in the background
shell: nohup java -jar app.jar </dev/null >/dev/null 2>&1 &
Ok So I had this issue today and my script is a tad different but essentially doing the same thing so this is what I have, running on Oracle, Centos and RHEL.
in script start_service.sh I have this, this script is executed not sourced.
#!/bin/sh
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
nohup java -jar ${DIR}/$1 --spring.config.location=${DIR}/application.yml --logging.config=${DIR}/logging.xml > ${DIR}/log/output.log 2>&1 &
In my playbook I have:
- name: run service
shell: "{{ service_install_location }}/application_name/start-service.sh application_name-{{ application_version }}.jar"
args:
chdir: "{{ service_install_location }}/application_name"
executable: /bin/bash
Now, the BASH_SOURCE argument is specific to bash so if you are running plain old sh then you need $0. nohup is required so when the ssh connection terminates your background task does not get killed (turn on -vvv for ansible to see connections being made to understand how this happens on a remote machine).
I should point out we are doing this temporarily as this should really be installed as a service to be under the control of service/init.d but for now, this answers your question

Log output shows in Jenkins while running bash script to execute Java code

At first let me describe my issue.
I configured Jenkins and after build action I called shell script to run bash script on remote server.
The shell script starts application via command
java -Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=xxx
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-XX:+HeapDumpOnOutOfMemoryError -jar name.jar "BUILD_PARAMETER"
I see logs from my application in Jenkins build, and it's keep build process running. I need to finish it after running
sh run command. Is it possible?
If you're doing this using Jenkins you will need to use the nohup notation as in the comments as well as specifying a non-numerial PID for the process. Jenkins tries to clean up after a job finishes by killing any processes it starts.
BUILD_ID=dontKillMe nohup <-your command -> &
the above command should work
https://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build
Your shell script need to fork a process, and return, otherwise Jenkins thinks your shell script is still running (which it is, if it's not forking the process and returning).
You have not provided the command you use to launch your application, but a common way to fork a process in linux is:
nohup <your command here> &

Java Heroku application run locally with foreman

I'm following the "Getting Started with Java on Heroku" guide at
https://devcenter.heroku.com/articles/getting-started-with-java
I follow the steps till I deploy and execute succesfully the application downloaded from GitHub.
When I try to execute it locally on Windows XP with the command
foreman start web
I get the error:
web.1 | started with pid 3388
web.1 | Error: Could not find or load main class Main
web.1 | exited with code 1
system | sending SIGKILL to all processes
My Procfile is:
web: java %JAVA_OPTS% -cp target\classes:target\dependency\* Main
And
>echo %JAVA_OPTS%
-Xms256m -Xmx512m
Can anyone suggest me how to solve?
Quotes and semicolon
web: java %JAVA_OPTS% -cp target\classes;"target\dependency\*" Main
I ran into this issue while running through the https://devcenter.heroku.com/articles/getting-started-with-java tutorial.
After tinkering with some of these answers I discovered that step six at https://devcenter.heroku.com/articles/getting-started-with-java#define-a-procfile tells the answer.
When you see instructions to run your app with foreman, append an extra -f Procfile.windows flag to ensure your Windows-specific Procfile is picked up. For example: foreman start web -f Procfile.windows
Once I switched to the foreman start web -f Procfile.windows command, everything worked smoothly.
Same problem with java-getting-stared app downloaded from heroku server. Changing to ";" works on Windows. Still need ":" on heroku linux server.

Categories