Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send messages to program through command line

I have this program, we'll call it Host. Host does all kinds of good stuff, but it needs to be able to accept input through the command line while it's running. This means it has to somehow send its other process data and then quit. For example, I need to be able to do this:

./Host --blahblah 3 6 3 5

This should somehow end up calling some function in Host called

handleBlahBlah(int x1, int y1, int x2, int y2){
  //do some more sweet stuff
}

Host is a C program, and does not need to support multiple instances.

An example of this is Amarok music player. With Amarok running and playing, you can type "amarok --pause" and it will pause the music.

I need to be able to do this in Linux or Windows. Preferably Linux.

What is the cleanest way to implement this?

like image 641
andrewrk Avatar asked Aug 13 '08 22:08

andrewrk


People also ask

How do you execute a program in a command line?

Type "start [filename.exe]" into Command Prompt, replacing "filename" with the name of your selected file. Replace "[filename.exe]" with your program's name. This allows you to run your program from the file path.

How do I send messages via remote desktop?

Chat with Remote Desktop clientsIn Remote Desktop , select a computer list in the sidebar of the main window, select one or more computers, then choose Interact > Chat. Enter your message, a line at a time. The message appears on the user's screen as you type. Press the Return key to complete and send each line.


2 Answers

If you were on Windows, I'd tell you to use a hidden window to receive the messages, but since you used ./, I assume you want something Unix-based.

In that case, I'd go with a named pipe. Sun has a tutorial about named pipes that might be useful.

The program would probably create the pipe and listen. You could have a separate command-line script which would open the pipe and just echo its command-line arguments to it.

You could modify your program to support the command-line sending instead of using a separate script. You'd do the same basic thing in that case. Your program would look at it's command-line arguments, and if applicable, open the pipe to the "main" instance of the program, and send the arguments through.

like image 187
Derek Park Avatar answered Sep 20 '22 03:09

Derek Park


If it needs to be cross-platform, you might want to consider making the running instance listen on a TCP port, and have the instance you fire up from the command-line send a message to that port.

like image 29
Stu Avatar answered Sep 19 '22 03:09

Stu