I'm trying to build a script that starts minecraft_server.jar (location: user directory / mineserver / minecraft_server.jar ).
I have PHP and Apache installed, and I'm trying trying to start the server JAR from /var/www/html/interface.php.
Via console, the server starts fine by running:
java -Xmx1024M -Xms1024M -jar mineserver/minecraft_server.jar nogui
... from the user directory. So in the interface.php file I have the following (note location listed above):
system('java -Xmx1024M -Xms1024M -jar /home/ec2-user/mineserver/minecraft_server.jar nogui', $retval);
But the server never starts after I visit the file. What am I doing wrong?
Thanks for any and all clues.
I want to say thank you for your help on Thanksgiving. While I haven't found the solution yet, I appreciate the efforts. Thanks again.
Have you checked the error log? It's at /var/log/httpd by default. If anything went wrong, it should be there. Otherwise, it could be starting wrong and I would recommend launching it in a screen so you can hop in and check on anything it's doing.
Install screen through whatever method your distribution uses, and then change the launch string to be:
screen -dmS "minecraft" java -Xmx1024M -Xms1024M -jar /home/ec2-user/mineserver/minecraft_server.jar nogui
The command will be executed with the permissions, and the environment of whatever user account the web server runs under.
In particular, it may have a different PATH entry, and the java command might not even be found, or it might be found somewhere else etc...
Also, be aware of what current working directory it is inheriting from your PHP script.
See exec() and check the output to help yourself do some basic debugging.
Also, you probably want something more like
exec('nohup java etc... > /dev/null 2>&1 &');
so that the process gets properly backgrounded and disconnected from the webserver parent process.
Related
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
The following process normally works for my startup scripts. However, when I introduce a command to execute a JAR file, it does not work. This script works while I am logged in. However, it does not work as a startup script.
In /etc/init.d I create a bash script (test.sh) with the following contents:
#!/bin/bash
pw=$(curl http://169.254.169.254/latest/meta-data/instance-id)
pwh=$(/usr/bin/java -jar PWH.jar $pw &)
echo $pwh > test.txt
Make script executable
In /etc/rc.local, I add the following line:
sh /etc/init.d/test.sh
Notes:
I make a reference to the script in /etc/rc.local, because this script needs to run last after all services have started.
Please do not ask me to change the process (i.e., create script in /etc/init.d/ and reference it from /etc/rc.local), because it works for my other startup scripts.
I have tried adding nohup in front of java command, and it still did not work.
Thanks
As written, there is insufficient information to say what is going wrong. There are too many possibilities to enumerate.
Try running those commands one at a time in an interactive shell. The java command is probably writing something to standard error, and that will give you some clues.
I am unable to run a crontab job , under a different user.(For e.g sudo -u someuser crontab -e)
It runs absolutely fine under my user profile.
I know what is the issue, but cannot find the resolution for it.
The issue is , when i configure this crontab job for other user, crontab is not able to find java ,as a result even simple java -version is not working.
Below is my script.
#!/bin/bash
export JAVA_HOME=/usr/jdk/jdk1.6.0_31
export PATH=/usr/local/bin:/bin:/usr/bin:/opt/dell/srvadmin/bin:/home/VishalS/bin
echo "JAVA_HOME is: " $JAVA_HOME >> log.out
echo "PATH is: " $PATH >> log.out
which java >> log.out
/usr/bin/java -version >> log.out
/usr/jdk/jdk1.6.0_31/bin/java -version >> log.out
output of above script :
JAVA_HOME is: /usr/jdk/jdk1.6.0_31
PATH is: /usr/local/bin:/bin:/usr/bin:/opt/dell/srvadmin/bin:/home/VishalS/bin
/usr/bin/java
so obviously, the below lines did not work.
/usr/bin/java -version >> log.out
/usr/jdk/jdk1.6.0_31/bin/java -version >> log.out
Could somebody please help me here? I do not understand why even after setting jdk path crontab does not executes java -version ?
Try setting the paths in the other users crontab directly. See 'man 5 crontab'.
The only that comes to my mind is that, the java command might not have the Executable permission to the user you are trying to execute it from.
So use chmod to give necessary permissions to execute.
Crontab(5) runs without ENV, so you need to source an environment (you are building the JAVA_HOME and PATH, but crontab gives you (almost) nothing. notice that the output of "which java" did not appear in your log file.
build an environment script you can source for your crontab script(s), ". path/to/env.sh"
Put full paths on all programs executed in shell scripts etc. You cannot rely upon environment in cron
run "which which", did you get /usr/bin/which? then put that in your script.
we often omit paths from scripts for convenience, but give paths in scripts run from crontab
does the java run when logged in as the other user?
Thanks everyone for your helpful comments.
However the actual fix which worked in my-case , was mix of steps as mentioned below :-
1. Setup Sun JDK path under root's user profile.(earlier open jdk was setup)
2. Gave permission to logs folder where logs were being written.(earlier the permission were not correctly set)
3. Tweaked my cronjob(i think there was an extra space there)
I'm running Ubuntu 10.10 with open jdk and crontab. I made a neat little Java application that does some cool stuff and exported it as a runnable jar file. I can run it from the command line just fine, but I needed something that would run the file once every day. So I wrote in a cron job to run a shell script that would actually invoke the jar file. The shell script works fine when run on its own and it looks something like this:
#!/bin/sh
/usr/bin/java -jar /root/proj/CoolStuff.jar
Works perfectly. So I added this to the crontab:
23 14 * * * /root/proj/runScript.sh > /root/proj/log.txt 2>&1
This does not run perfectly. In fact, it doesn't run. log.txt will come out saying "Error". Pretty nondescript, right? I've checked my environment variables, nothing fancy there. Is there anything else I might be missing? Any ideas as to where to go from here? Is there any way I can run this script everyday with ease?
Check the execution permission on that file.
The cron is running with different permissions, not the one you got when logged in.
Also you try to access /root.
Try to relocate your script to another "non-root" directory.
See what the environment for you crontab looks like by commenting out your current /usr/bin/java .. and insert set on a line by its own.
Now from you command line do set > tmpEnvVarList.txt and compare with what you see in log.txt.
It's almost certainly that your .profile or .bash_rc (or other) is setting env vars that are not available to you crontab. You'll need to add . .profile etc to you script.
cbO's ideas are good too.
I hope this helps.
I want to run -jar file from my php script, my -jar file run perfectly through commandline but i want to integrate it to my website but it is not working properly and also not provide me any error. my code is below
if (isset($_POST['Export'])) {
exec('java -Xmx1024M -jar C:\UploadTest-1_2.jar http://localhost:8080/packaging/Package C:\book\testbook.pdf –pass park345');
echo 'export button clicked';
}
output is:
export button clicked but -jar not doing needful.
Is Java on the path?
Try the command with "java -version", if you don't get output, Java is not on the PATH defined within the PHP environment.
Is the environment locked down?
When running PHP code, one generally doesn't allow any kind of execution to take place, as people could attempt to hijack the PHP environment to place code on the server which wasn't there. This means that you will have to take into account any type of chroot'ing, acl controls, permissions, selinux, and web server configurations which make it harder to run items without a clear knowledge (that means prior configuration) that the application should be allowed to run.