[SOLVED] Bash shell issuing commands to the process it starts
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash shell issuing commands to the process it starts
Hello!
I have a shell script to start Minecraft. It works great, but I want to be able to send commands to Minecraft from the shell script. For example:
Code:
#!/bin/sh
java -Xms512M -Xmx4096M -XX:MaxPermSize=256M -jar minecraft_server.1.8.7.jar
sleep 10
echo "say This is a test.\r"
The java process starts, but it never gets to the echo. Is there a way to accomplish this? Do I need to start a second script and grab the PID of the java process? Something to do with screens? I've tried a few different things but can't for the life of me figure out where to go from here.
Do you know how to send commands to Minecraft? Or that Minecraft would be listening or capable of accepting messages?
The echo would work once you put java in the background as shown, but I don't know whether or not the Minecraft game itself would accept messages if the intent is to send external messages into that applet.
I know Minecraft can accept commands from shell. Here's a github repo of a web interface that uses shell to issue Minecraft commands: https://github.com/Thue/rfwadmin
I'm trying to dissect it right now. The separate threading with & worked, but as you pointed out the echo wasn't accepted by Minecraft.
I know Minecraft can accept commands from shell. Here's a github repo of a web interface that uses shell to issue Minecraft commands: https://github.com/Thue/rfwadmin
I'm trying to dissect it right now. The separate threading with & worked, but as you pointed out the echo wasn't accepted by Minecraft.
The echo goes to the standard output of your shell script. Since minecraft is not reading from the standard output, it doesn't receive the echo.
minecraft might read from some IO channel. You have to find out what that channel is. Next step figuring out how to write to it.
Actually echo might be exactly what would work for you. For instance if you wished to send a character over a serial connection, such as /dev/ttyS0, you could choose to do:
Code:
echo "hi there" > /dev/ttyS0
and then this would be sent out via that serial connection. Therefore if there were a named pipe where Minecraft accepts these commands, echo and the output redirector would do the job. Similarly there are things like /proc or /sys resource files where if you need to change a setting, the resource is read-only. Something like the CPU speed adjust, stuff like that. You would echo a ONE or a ZERO to the file, but not be able to read it back, however it would institute a change in the controls of the system.
Turns out I was making this harder than it needs to be. Minecraft implemented rcon, so I can communicate with the server without a shell script: https://github.com/thedudeguy/PHP-Minecraft-Rcon
In case someone finds this thread on Google and really does want to use shell scripting, I also got shell working with tmux. If you start the server using tmux (tmux new -a minecraft), you can control it with a shell script like this:
Code:
#!/bin/bash
tmux send-keys -t minecraft "say Hello World!" Enter
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.