How to give realtime input to a background process?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
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.
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).
Code:
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.
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.
somecommand &
jobs
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.