I am working on a minecraft control panel in Ubuntu and thus I need to start/stop a .jar filewith shell_exec();
When I try commands like "whoami", the output is normal. But when I try this:
shell_exec("screen -dmS mcsrv java -Xmx512M -jar /var/www/srv/craftbukkit.jar -o true nogui");
It does not do anything, I have checked the permissions too and www-data is the owner of the files
Try to redirect the standard error stream to stdout (by appending 2>&1 to the command), fetch that output and print it to check whether there was a meaningful error message
$cmd = "screen -dmS mcsrv java -Xmx512M -jar /var/www/srv/craftbukkit.jar -o true nogui";
$redirect = '2>&1';
// using variable substitution only for readability here
shell_exec("$cmd $redirect", $output);
var_dump($output);
Related
I am running java -jar xyz.jar command on the linux terminal. I have both system.out.println statements and System.exit statements in the code.
how to capture the system.exit or output to the OS on the linux? do i need to prepare a linux script for this?
This may be off-topic for SO.
You just redirect it:
java -jar xyz.jar > the_file_to_output
If you used System.err output as well as System.out output and wanted to redirect both, it's a tiny bit more complicated because you need to redirect out to the file and then redirect error to out:
java -jar xyz.jar > the_file_to_output 2>&1
The 2>&1 thing is what redirects error (standard stream #2) to out (standard stream #1). Note that the order matters, you have to do that after redirecting out to the file.
Java works as any other command.
If you want standard output (or error output) you can work with usual I/O operators: > 2> >> |
java -jar xyz.jar > output.file
java -jar xyz.jar | sort | less
For getting System.exit value you have special variable $?
java -jar xyz.jar
echo $?
To get more info Exit status and I/O redirection
I would like to run a Minecraft server in the background with Ubuntu, but every time I do it stops running.I am also running bash as my shell.
java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui &
When I run jobs I get this at first.
[1]+ Running java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui &
Then this a few seconds later.
[1]+ Stopped java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui
Later I found out that it will stay "Running" if you redirect stdin/stdout/stderr to some files.
java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui > out.txt 2> err.txt < in.txt &
It works great, but I want to use the in.txt file as input for the running java process. When I first run this it executes the in.txt once. So if I have /stop in in.txt it will stop the server. But ONLY once. I would like to put commands in the text file and have that passed to the minecraft server. The file would become my keyboard input while the process is running. I know I can use screen, but I want to see if I can run it with a file as input. Again the in.txt would be my input.
So I could do something like this.
echo -e "/stop\r" > in.txt
OR
echo -e "/time set 0\r" > in.txt
Another rabbit hole.
I even tried making my own pipe. This makes minecraft run in the background.
mkfifo pipe;
java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui &> out.txt < pipe &;
cat > pipe
If I cat > pipe my prompt is taken again, but I can enter commands and it executes them as stdin. So if I do /say hi it will run it. Awesome! One problem. I need my bash prompt back. If I close it by using Ctrl-C, I'll lose my stdin forever! I can keep it around by Ctrl-Z. Later If I need it I'll fg. But that is very crude. I would like to run a script that would stick something into that at a later time. Something like echo "/say hi" > pipe. I lose stdin if I don't keep something running. echo runs once. That's the problem, it collapses stdin.
I am a novice at bash scripting so a little help will be greatly appreciated.
You already got the idea of using a FIFO. You need to run it with the following command:
while true; do cat pipe ; done | java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui > out.txt &
The while loop will make sure that even if the process you use for writing to the FIFO closes the file, the JVM standard input will not be closed. It will just start catenating pipe to it again and the while`s standard output is considered one long stream.
I have a .jar file I want to run whenever the system reboots/starts, so I put the line
nohup java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
in my /etc/rc.local file. The program is validated as working, and if I run the above command at the command line the program works as expected.
Other versions I have tried without success:
nohup /usr/bin/java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
and:
nohup java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar 2> /dev/null \ .. &
I am running centos 6.4.
Check that your jar file is accesible roots, NFS mounted volumes may impose special restrictions for root.
Instead of discarding your error messages, you might want to route them to syslog, something like 2> /sbin/logger -t FOO 1> /sbin/logger -t BAR
Maybe the path isn't set yet at startup time and you need the full path to the java executable or, possibly, nohup.
I am starting a java programm under Red Hat Enterprise Linux Server release 5 (Tikanga).
directory structure:
- bin ->sc.jar,start-sc.sh,sc-lib-all.jar
- conf->log4j-sc.properties,sc.properties
command to run the java programm (which is perfectly working):
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:../conf/log4j sc.properties -jar sc.jar -config ../conf/sc.properties
if i put it into a shell script the java programm can't find the prop file anymore.
shell script (start-sc.sh) looks like:
#!/bin/sh
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:../conf/log4j-sc.properties -jar sc.jar -config ../conf/sc.properties
i am a newbie on shell scripting any ideas what i am missing? thx!
i guess you started your shell script not from the bin directory, which the dir start-sc.sh belongs to.
to explain it clear, let's make an example.
say, your script is here:
/foo/bar/bin/start-sc.sh
if you start it under /foo/bar/bin/, it (the relative path) should work.
but if you start your script from /home/yourHome/someDir/ , the relative path will point to $PWD/../, which is /home/yourHome/
you could either in your script first cd /foo/bar/bin/ before you start the java app. or do something like:
a=`dirname $0`
if [ $a = '.' ];then
a=`pwd`
fi
cd $a
/usr/java/jdkxxxx/java .....
It sound fine to me, does this version work?
#!/bin/sh
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:$(pwd)/../conf/log4j-sc.properties -jar sc.jar -config $(pwd)/../conf/sc.properties
Edit #1:
Try put the following before launching your program:
echo `pwd`
The output tells you where you are running your script, so you can check if it's the right path or not.
Edit #2:
Try this script
#!/bin/bash
LOG4JCONF="/absolute/path/to/the/log4j/conf/file"
SCCONF="/absolute/path/to/the/other/conf/file"
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:$LOG4JCONF -jar sc.jar -config $SCCONF
I have a Java program Desktop/testfolder/xyz.jar on a remote machine. It has a configuration file on the same folder. When I SSH into the machine, I do:
"ssh user#remote java -cp Desktop/testfolder/xyz.jar Main"
The problem here is the configuration file is not in the path, as we are in the home folder so my program cannot read the configuration.
I want to first go into that folder and then run the program from that folder. In a shell script if I did this
"ssh user#remote cd Desktop/testfolder"
"java -cp xyz.jar Main"
it executes the first statement and when the second statement is run it runs on my current machine not the remote machine.
Can we do only one command or there are any other solutions for this?
Try something like this:
ssh you#yours.com "cd /home && ls -l"
You could try separating the commands by a semicolon:
ssh user#remote "cd Desktop/testfolder ; java -cp xyz.jar Main"
If you want to split your commands over multiple lines for the sake of readability, you could also pass the list of commands to the bash command as follows:
ssh user#remote.host bash -c "'
cd Desktop/testfolder
java -cp xyz.jar Main
'"