Solved

Sending commands to a program in the background

Posted on 2011-02-24
35
673 Views
Last Modified: 2013-12-29
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
Comment
Question by:techport13
  • 10
  • 7
  • 5
  • +6
35 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 34976655
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34976664
Do you have access to java code of the program and do you have the ability to modify the program?
0
 
LVL 92

Expert Comment

by:objects
ID: 34976680
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
 

Author Comment

by:techport13
ID: 34976683
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
 

Author Comment

by:techport13
ID: 34976692
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
 
LVL 92

Expert Comment

by:objects
ID: 34976712
> 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
 
LVL 35

Expert Comment

by:mccarl
ID: 34976730
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
 

Author Comment

by:techport13
ID: 34976745
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34976772
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
 
LVL 35

Expert Comment

by:mccarl
ID: 34976774
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
 
LVL 92

Expert Comment

by:objects
ID: 34976807
see my second example, not the first one


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

Expert Comment

by:for_yan
ID: 34976936
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
 

Author Comment

by:techport13
ID: 34976984
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
 

Author Comment

by:techport13
ID: 34977042
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
 
LVL 35

Expert Comment

by:mccarl
ID: 34977073
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
 

Author Comment

by:techport13
ID: 34977171
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
 
LVL 35

Expert Comment

by:mccarl
ID: 34977339
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Accepted Solution

by:
Anton74 earned 500 total points
ID: 34977370
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
 
LVL 6

Expert Comment

by:de2Zotjes
ID: 34977652
I see 1 basic error in the last author comment:

echo /bukkit/wrapper |

that should read

cat /bukkit/wrapper |

0
 
LVL 19

Expert Comment

by:simon3270
ID: 34978374
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
 
LVL 19

Expert Comment

by:simon3270
ID: 34978375
sorry, typo: "kibbutz" -> "kibitz"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34979651
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
 

Author Comment

by:techport13
ID: 34982679
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34982733

Can you explain what is the difference between screen and VNC from the point of view of functionality which you need?
0
 

Author Comment

by:techport13
ID: 34982751
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
 
LVL 9

Expert Comment

by:Anton74
ID: 34982783
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34982814
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
 

Author Comment

by:techport13
ID: 34982834
I don't have another linux system available.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34982848
For what purpose would you need another linux system?
0
 
LVL 9

Expert Comment

by:Anton74
ID: 34982936
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
 

Author Comment

by:techport13
ID: 34983402
Anton,

Yeah I found the GCC complier but I'm having issues with getting apt-get installed properly on DSL.
0
 
LVL 5

Expert Comment

by:josephtsang
ID: 35165204
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
 
LVL 5

Expert Comment

by:josephtsang
ID: 35165210
Check the wiki on the file permission I just explained.

http://en.wikipedia.org/wiki/Setuid
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35171355
>>
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35172291

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now