[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 788
  • Last Modified:

Sending commands to a program in the background

Experts,

I know just enough about linux and similar os's to get myself into trouble.  Here's the scenario.

I have a JAR file that I run with the following shell script:
==== start.sh ====
#!/bin/bash
/usr/java/java -jar /prog/ram/jarfile.jar

Open in new window


After starting, I can enter "commands" to the program via the terminal.  Basically an admin console for a server app.

My question is this.  1) I may need more than one person to be able to send these console commands to said server, 2) I certainly want to be able to run this in the BG via a daemon so that I don't have to be logged in for it to be running.

The problem is that only the user who started the server can access the console, and if I move it to the bg, no one can send commands.

I'm wondering if there's perhaps some way to send commands to it via stdin?  I have searched wide, but not far, and I haven't seen anything that I think will help, likely because I don't really know what I'm searching for.

Thanks in advance!
0
techport13
Asked:
techport13
  • 10
  • 7
  • 5
  • +6
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Do you have the source code for the jar that you are running? If so you could probably write other Java code that uses that jar and gets the commands to it in another way. (It may even be possible to do that without the source code, but it would depend on how it was written)

Otherwise, I could suggest that maybe netcat may be able to help you. I am pretty sure that netcat can start up as a TCP socket server and pipe anything that it receives over a socket to its stdout, then you can just pipe that to you jar, eg.

nc <options> | /usr/bin/java -jar /prog/ram/jarfile.jar

It probably would take quite a bit of tweaking though to get it doing the right things. I don't have a linux box near me at the moment to test some things out for you.

Hope this gives you some ideas...
0
 
for_yanCommented:
Do you have access to java code of the program and do you have the ability to modify the program?
0
 
objectsCommented:
you can pipe a file into stdin using something like:

/usr/java/java -jar /prog/ram/jarfile.jar < input.txt

or pipe output of another app

/abc/someexecutable | /usr/java/java -jar /prog/ram/jarfile.jar

not sure how you intend to achieve your goal though
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
techport13Author Commented:
I do have access to the source, but as much as I am a novice with linux I am even LESS familliar w/ java code.

I would prefer a way to do this that did not require modifying the application in question.
0
 
techport13Author Commented:
objects,

I need to be able to send commands to a running process, not just at launch...

IE:
I run start.sh, which launches the server jarfile.  It outputs various messages to the screen and then waits there for input.  If I type stop for example, the server will shutdown.  Or i can type various admin commands to affect the server.

0
 
objectsCommented:
> I need to be able to send commands to a running process, not just at launch...

then you use a process that would write the commands to stdout as required, as in the second example aove
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
As I said then, look into netcat (nc) in linux.

But another question remains... how do you see these other people sending the commands to the server? ie. are they also logged into this linux machine? should they do this from a webpage? etc, etc
0
 
techport13Author Commented:
I guess I'm not not understanding the whole concept?  Or perhaps I'm not explaining my situation properly.

java -jar jarfile.jar is the process i need to send commands to.

if i launch

java -jar jarfile.jar < somefile

and then i write to somefile via

echo somecommand > somefile


Will somecommand end up getting into the process that is running java -jar jarfile.jar???
0
 
for_yanCommented:
Does it take some input then executes for some time, then waits for new input, and then executes again when this new input arrives?
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
No, only whatever is already in somefile will be passed into the process. Writing to it afterwards won't send anything additional into the process.

That's why both objects and I said you have to have a process that pipes into the java process (not just a static file)

nc <option> | java -jar jarfile.jar

This way nc (or whatever you can find to do what you want) stays running and so can continuously send input into the java process. You may be able to do something with a FIFO to do sort of what you are asking. ie..

create a FIFO (I can't remember exactly but check out mkfifo)

echo myFifoFile | java -jar jarfile.jar

and then you should be able to do..

echo "stop" > myFifoFile

to send it a stop command
0
 
objectsCommented:
see my second example, not the first one


/abc/someexecutable | /usr/java/java -jar /prog/ram/jarfile.jar
0
 
for_yanCommented:
Let me give you unconventional suggestion.
Install VNC server on this linux machine and run your server in VNC terminal.
This terminal will not bother you on the screen, and, more importantly, you or whoever you choose to charge with
it, will be able to access it from any platform and even from mobile and feed in the next
input. It is true it will probably be waiting more often than when you'll be reading from some file or pipe, but this system
will address at least some of your concerns, as far as I understood them.
0
 
techport13Author Commented:
for_yan,

Yes, that appears to be the way it functions.  If i execute a command that takes a bit of time to run, I'm not able to enter another command until the first finishes.

Checking out myfifo now.
0
 
techport13Author Commented:
for_yan,

vnc is not an option for me, but thanks for the suggestion.

So this is where I'm at so far.

I created a fifo via:

mkfifo wrapper

Then ran

wrapper | /usr/java/bin/java -Xms1024M -Xmx1024M -jar /prog/ram/somefile.jar &

this allowed me to run the whole shebang in the background even after logging out of the ssh session,

but does not appear to be accepting commands via

echo "somecommand" > /path/to/wrapper

Is there any way for me to easily determine if my commands are getting to the running jar but not being run - vs. not getting there at all?
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Did you try it with "echo" in front of "wrapper" like I showed above, so...

echo wrapper | /usr/java/bin/java -Xms1024M -Xmx1024M -jar /prog/ram/somefile.jar &

You should be able to see what is going through the pipe by doing something like this...

echo wrapper | tee /tmp/command_log.txt | /usr/java/bin/java -Xms1024M -Xmx1024M -jar /prog/ram/somefile.jar &
 "tee" will write what ever comes into it to that file while also sending it on to stdout, which we pipe into the java program. So you should be able to see your commands that you "echo" to the fifo, come up in that file
0
 
techport13Author Commented:
okay enough of the random names, makes this much more difficult.

# mkfifo wrapper

# chmod +x wrapper

# echo /bukkit/wrapper | tee /bukkit/testlog | /usr/java/bin/java -Xms1024M -Xmx1024M -jar /bukkit/craftbukkit-0.0.1-SNAPSHOT.jar &

#cat /bukkit/testlog
/bukkit/wrapper

# echo "testing" > /bukkit/wrapper (hitting enter moves cursor to next line but does not return me to the prompt)
hello
goodbye
ctrl+c

ctrl+c displays -bash: /bukkit/wrapper: Interrupted system call

#cat testlog
/bukkit/wrapper



0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
It seems as if the FIFO is blocking for some reason. I will try some stuff out later when I have access to a linux box and let you know.
0
 
Anton74Commented:
If I'm understanding this correctly, this is running in a console on the command line.

There is actually a pretty simple solution that will both allow it to continue to run when your session disconnects, and also allow multiple people to access the same session running the console (at the same time even).

It is the "screen" utility. If this works for you, no modification or wrappers are needed for the program in question.

I have written a brief article on basic usage of this tool: http://www.experts-exchange.com/OS/Linux/Administration/A_4595-CLI-sharing-and-session-management-in-Linux-with-'screen'-A-Quick-Start-Guide.html
0
 
de2ZotjesCommented:
I see 1 basic error in the last author comment:

echo /bukkit/wrapper |

that should read

cat /bukkit/wrapper |

0
 
simon3270Commented:
How about "expect"?  The kibbutz expect script allows two processes to share stdout and stdin, so it should be fairly easy either to use that directly, or to use the same principles to allow anyone to connect to stdout and stdin of the running Java program.
0
 
simon3270Commented:
sorry, typo: "kibbutz" -> "kibitz"
0
 
CEHJCommented:
Try the following, then check the contents of 'testlog'
# mkfifo /bukkit/wrapper

#/usr/java/bin/java -Xms1024M -Xmx1024M -jar /bukkit/craftbukkit-0.0.1-SNAPSHOT.jar </bukkit/wrapper | tee /bukkit/testlog &


# echo -e "Just\ntesting" >/bukkit/wrapper

Open in new window

0
 
techport13Author Commented:
CEHJ,
That example did not write anything to testlog.

Other things to note:
I am running a damnsmalllinux distro and screen is not installed.  Nor is gcc so that I could download and compile screen.  I'm in the process of pouring over the DSL documentation/help to see if I can figure out how to get screen running - which sounds like it'll be very close to what i'm looking for.
0
 
for_yanCommented:

Can you explain what is the difference between screen and VNC from the point of view of functionality which you need?
0
 
techport13Author Commented:
I don't want to run X on this box if I don't have to.  It's seems like serious overkill to run VNC + terminal session in X just to run a CLI interface remotely.
0
 
Anton74Commented:
I'm not familiar with DSL, but could you compile a statically linked screen binary on another system, and put it in /usr/local/bin/ for example?
0
 
for_yanCommented:
OK, I understand, I didn't know you were not running X, but the basic thing is still similar - still no way to get into the process itself, but just
to get access to the screen - which will already help you.  
0
 
techport13Author Commented:
I don't have another linux system available.
0
 
for_yanCommented:
For what purpose would you need another linux system?
0
 
Anton74Commented:
There seem to be development packages available in DSL's System Extensions:

http://distro.ibiblio.org/pub/linux/distributions/damnsmall/mydsl/system/

Perhaps the gcc1-with-libs.dsl and possibly libc6-dev.dsl packages would allow compilation of screen on DSL itself.

If needed I can look into this and/or the statically linked binary a bit more, but I'll need some time.
0
 
techport13Author Commented:
Anton,

Yeah I found the GCC complier but I'm having issues with getting apt-get installed properly on DSL.
0
 
josephtsangCommented:
My question is this.  1) I may need more than one person to be able to send these console commands to said server, 2) I certainly want to be able to run this in the BG via a daemon so that I don't have to be logged in for it to be running.

The problem is that only the user who started the server can access the console, and if I move it to the bg, no one can send commands.

Would you check the file permission on your start.sh? Is it appearing like this:

-rwsr-s--- admin admingroup start.sh

Open in new window


where it grants the ownership of the started-up process to admin/admingroup no matter who  started/executed it (in this case you may let it started by the background daemon). At the same time grant every one in the admin group the execute right such that they may connect without problems.
0
 
josephtsangCommented:
Check the wiki on the file permission I just explained.

http://en.wikipedia.org/wiki/Setuid
0
 
CEHJCommented:
>>
CEHJ,That example did not write anything to testlog.
>>

Did you get any error messages? That approach works for me. You'll need to make sure you're able to create the pipe and have permissions to write to it
0
 
for_yanCommented:

Well, after all, to install minimal X on DSL and then run VNC is probably the simplest solution, which will
still be of use for access by several users, even though would not allow to get into the process itself.

They even mention VNC on DSL Wiki:

http://www.damnsmalllinux.org/wiki/index.php/FAQ

0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 10
  • 7
  • 5
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now