Send a Signal or Data to C++ Program from Bash Script

bashcrhel

As the title states I would like a to be able to send a signal, alert, message, or something to a C++ program while it is executing from a bash script. I have seen some solutions where a script will start the program with arguments, but I need it to occur while the program is already up and running. Is there any way to do that? Basically while the script runs in the background if an error occurs I would like it to be reported to the program directly, rather than be logged somewhere in the OS. I am using Redhat 8.

Thank you

Best Answer

To send signal:

kill -s signal -- pid_of_your_app

signal - Is the signal to send. It may be given as a name or a number.

To send data:

echo "text_message" | your_app

or

cat file | your_app

| - Is pipe, it will send stdout of one command to stdin of your program

Update:

About signal handling, you app needed to handle the signal, because by default it will just exit (you can assign handler for any signall except kill(9)) https://en.cppreference.com/w/cpp/utility/program/signal

#include <csignal>
void signal_handler(int signal);
// Install a signal handler
std::signal(SIGINT, signal_handler);
Related Question