-   Linux - General (
-   -   How to give realtime input to a background process? (

metaf5 11-20-2011 08:45 PM

How to give realtime input to a background process?
So basically what is happening is I will start a process from a script. In this case the process is a Minecraft server, which is a java application. When I start it normally, my terminal will stay connected to the process, and I can enter commands to the application, like "stop" or "list users".

So, when I've started this application from a script, I don't have it open in a terminal to give it input, is there any way to 're-open' it, so me (or another script), could give this application commands? Ideally, I'd also be able to read the output it gives immediately after I give it a command.

I've heard I could use screen to do this, but is there any more elegant way? Screen seems a bit roundabout.

sag47 11-20-2011 09:11 PM

I recently read the dd man page for running dd in the background. It mentions running the following kill command to bring it into the foreground and print out statistics of it and let it continue (does not kill it).


kill -USR1 pid
This assumes that you launch the command into the background using the current shell. If you sub-shell it (i.e. su - user -c somecommand) then that kill command won't work. I've tried it.

The kill man page mentions the -USR1 switch but does not specify what it is used for. So I still need to research more into it.


metaf5 11-21-2011 02:22 PM

Based on my very limited knowledge of the topic, that actually sounds brilliant.

It's my understanding that when a process goes to the background, it forks off, and runs under user id 1, and kills the old process. So, it looks like this is just killing the process under UID 1, and restoring it. That's pretty sweet. Could do that in a script, and then give input to (what I think is) it's STDIN?

Edit: I'll give it a try before decrying it, but it looks like this is merely sending it a "User Defined Signal", and according to wikipedia, dd behaves notably in this manner, and it's unclear what this will do to other applications.

sag47 11-21-2011 03:21 PM

Does normal process control not work for you?


somecommand &
fg 1
#press CTRL+Z to put in the background paused
bg 1

Also you might want to look at screen for session management.

Edit: I just reread your first post and saw you already discussed screen. You can pretty much disregard this post but I'll leave it up for archival purposes.

metaf5 11-22-2011 02:56 PM

Yeah looks like I have to use screen. (Just figured I'd post in case someone stumbles upon this thread in the future)

All times are GMT -5. The time now is 01:05 AM.