[Ubuntu 14.04] I have a java console application, and I need to run all time (1st problem), as well as I can re-access the console whenever I want (2nd problem).
To solve the second problem, I use a screen, then run the jar file in it. So that I can re-attach the screen to access my console app.
I am now stuck with the 1st issue. I want to make the screen autostart with OS. Because I need the app running all time. Anyone give me an idea? I appreciate all your help. Thanks.
UPDATE 11/17/2015:
With #janos's help, it works fine except using #reboot. I tried crontab starting the script each 5 minutes and it worked fine. But when i replaced it with #reboot, it did not work anymore!
Finally I chose this solution: Create a screen with a particular name, then create a script sh file to check if the screen with that name existed or not. If not, then run the screen along with java file. Last, create a crontab to run the script each 1h.
Many thanks to #janos for your effort and help.
To run a program after system boot, use #reboot in your crontab:
#reboot /path/to/executable args
For more details, see this Ubuntu help page:
https://help.ubuntu.com/community/CronHowto
As per your comments, you seem to be having difficulty running Java + screen with cron. To help you debug, I suggest to create a custom configuration file for screen, let's call it ~/screen-debug, with a content like this:
screen -t home
screen -t java bash -c 'java -jar ...; echo Press enter to exit; read'
And use a crontab line like this for testing:
*/5 * * * * screen -c ~/screen-debug -d -m -R java
What's happening here:
Run the job every 5 minutes
Use a specific screen configuration
Start screen in detached mode
Reuse the screen session named "java"
If a session with this name doesn't exist, it will create it
If a session with this name doesn't exist, it will reuse it (not start another screen)
There will be two windows in the screen session:
Labeled "home": a simple shell, as if you run screen in your home directory
Labeled "java": the Java program, hopefully happily running. If not happily running, you should see the error message that should help you debug the problem, and a prompt to "Press enter to exit". When you press enter, the shell will terminate in this window.
Once you get this working, then you can replace */5 * * * * with #reboot.
Related
I have a script to start my Minecraft server on a Raspberry Pi 4. It has to run in a GNU screen because of my backup utility. I have already tried specifying a path and it shows up in the log as a process but it doesn't run.
I would really appreciate some help on this because my search history is so full trying to find the issue but nothing works. The Bash script is as follows:
#! /bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
cd /home/pi/Desktop/Servers/PvP
screen -S pvp java -Xmx2560M -Xms2560M -jar server.jar nogui
My crontab is as follows. I have a new line below this entry so its not that
00 03 * * * ./start.sh
I've tried specifying the full path and that doesn't work either
Edit:
I also created restart.sh which simply calls stop.sh and the calls start.sh
stop.sh is very simple
killall screen
This then calls start.sh. The entire restart.sh script is also very simple
./stop.sh
./start.sh
I put this in my crontab and started a screen session. Low and behold, at the time I set the job to start, screen terminates. I then type screen -ls, expecting to see the newly created window created by start.sh but it has not run. Its beyond me why this is not working. Everything works if done manually, please help this is DOING MY HEAD IN! AAAAAAH!
Specify the actual full path, which must start with a slash. For example, 0 3 * * * /home/username/project/start.sh
Please excuse me for my inexperience, I am very new to this.
I set up a server on Digital Ocean for Minecraft earlier today. I ran the command to create the world with java -Xmx1024M -Xms1024M -jar minecraft_server.1.15.2.jar nogui, and created a screen.
It gave me access to the console where I could type like /op and other commands. Later on, I shut down, and now when I try to rejoin, I'm not sure how to get back to that console. I have more than 1 person on the server, who is on the console right now - I don't know if that should cause it any issue. When I retry to open the console with the command java -jar minecraft_server.jar It says there is already a server running (which there is) but I just want to get back to the console.
Thank you in advance.
If this is a linux vserver or root server, you can use the "screen" tool to access the minecraft console again.
Create a new startup script (start.sh) in the server directory to launch the JAR:
#!/bin/sh
screen -d -m -S "minecraft_screen" java -Xmx1024M -Xms1024M -jar minecraft_server.1.15.2.jar nogui
Open your terminal and execute the following in the directory:
chmod +x start.sh
Run your start up script:
./start.sh
To access the minecraft screen use the command "screen -r".
To leave the minecraft screen press Ctrl + A + D
As a player logged into the server, stop the server by typing /stop. This will stop and close the server. Now proceed back to your hosting service and start the console the same way you did the first time. If it worked this way the first time, i can imagine itll work the second time.
just noting in case this becomes a resource for other new server owners, as of writing this comment - screen has been depreciated and tmux is it's replacement
use 'tmux' to create a screen
use 'ctrl+b' then 'd' to detach
use 'tmux attach' to rejoin a screen
i spent a little while searching the internet for this, so i thought sharing it here would be logical - apologies if this question is too outdated to still be relevant
Below are the batch file code.....
cd\
cd C:\Program Files\Project
"C:\Program Files\Project\jre\bin\javaw.exe" -classpath .;Project-jar-with-dependencies.jar;javafx-2.2.jar; com.ui.main.Main
Problem:
When i click on the batch file, CMD window open (black screen) but i want to execute that batch file silently in background means CMD window should not open.
A few suggestion are mentioned on Google but that execute the batch file using VB script but i do not want that solution.
Any suggestion. Thanks in advance.
Some simple answers that don't exactly "not open" but may be close enough.
create a shortcut to your batch file and in its Properties change Run to Minimized, and use it. The CMD window appears in the taskbar but not on the desktop.
since you only want to run one (nonconsole) executable, use 'start' to start it, then the batch file exits while the program (javaw) continues running. The CMD window flashes briefly and then disappears.
do both. The CMD window flashes briefy in the taskbar.
Also: If you are (or will be) using JRE 7 from Sun^WOracle, it now has javafx included and you don't need your own copy.
A .bat file needs cmd.exe in order to get processed. There is no way to hide its output window if you are executing it without an external host (like you've mentioned about VB). There would be different approaches to hide the window when it shows up, but this would require another application and be a very messy solution.
#echo off > nul
should do the trick (put it at the beginning of the batch file
I've been coding a litte bash script which connects on several distant servers, then execute a java CLI program through a few expect instructions.
It goes like this :
bash script
expect
ssh to server using public keys
expect ...
expect ...
log_file my_file (everything displayed on the screen is now redirected to my_file)
expect ...
log_file (closing my_file)
exit
exit
When I execute my script manually everything runs OK.
When I execute it through crontab, the file my_file is empty.
I found out that cron jobs don't have a tty attached and that PATH isn't the same as usually
My question is : is there a way to force the creation/allocation of a tty to my cronjob?
I've tried using the -t and -tt option with ssh but no result.
redirecting standard output on different levels of the script didn't work.
Also, I can't install screen (which could have helped, maybe) and "script" isn't writing anything either.
Thanks a bunch!
You can check the cron tab log for erros and make sure the full path is given for the command to be executed.
Is it possible to execute a Java program in the background so the user can execute other commands in front of it?
For instance, here is how the console might look for said program:
$ myProgram (executes program)
Program Started! (output from myProgram)
$ (the user can enter another UNIX command while myProgram is still running)
Thanks in advance!
Background execution is part of the shell. You can add & at the end of the command line to run it in the background.
The background output does not go to the current shell. If that happened, it would be confusing to the user, having to type input while the terminal is still producing output.
EDIT: I just tried "ls &" on cygwin, and the ls output appears in the console. Seems there is a lot of conflicting information on the net! :)
Best way is to use screen if you dont have it type
sudo apt-get install screen
type
screen
run the command like
java MyClass
press ctrl + (a + d)
to view this window again, type screen -x
I beleive it's possible to start the program and allow access to the shell... but the user would not see the output.
I do not think its possible to achieve the example scenario you have given.