BoosterSeat  0.1
A C++ library that includes common utilities that are used in other projects.
[BST module] Process

The bst process class is a simple interface to run linux commands.

This is a major refactor of the project cpp-subprocess.

Detailed class documentation can be found here.

Example: Basic usage

#include <iostream>
int main() {
bst::Process process("echo", {"Hello, World!"});
process.waitToComplete();
std::cout << process.getExitCode() << std::endl;
std::cout << process.getStdout() << std::endl;
return 0;
}
A class to handle linux process calls.
Definition: process.hpp:33
void waitToComplete(unsigned int timeout=1000)
Waits for the process to complete. If the process does not complete within the timeout,...
Definition: process.cpp:64
A class to handle linux process calls.
int main()
Definition: visual_tests.cpp:10
$ ./example
0
Hello, World!

Example: Write additional input to stdin

#include <iostream>
int main() {
bst::Process process("cowsay", {});
process.writeToInStream("hi there\n");
process.closeInStream();
process.waitToComplete(); // default timeout is 1000ms
std::cout << process.getExitCode() << std::endl;
std::cout << process.getStdout() << std::endl;
std::cout << process.getStderr() << std::endl;
}
void writeToInStream(const std::string &s)
Definition: process.cpp:34
$ ./example
0
__________
< hi there >
----------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||

Example: Non-zero command exit codes

#include <iostream>
int main() {
bst::Process process("ls", {"/nonexistent"});
process.waitToComplete();
std::cout << process.getExitCode() << std::endl;
std::cout << process.getStdout() << std::endl;
std::cout << process.getStderr() << std::endl;
return 0;
}
$ ./example
2
ls: cannot access '/nonexistent': No such file or directory

Example: Commands that do not exist

#include <iostream>
int main() {
bst::Process process("this_command_does_not_exist", {});
process.waitToComplete();
std::cout << process.getExitCode() << std::endl;
std::cout << process.getStdout() << std::endl;
std::cout << process.getStderr() << std::endl;
return 0;
}
$ ./example
22
bst_process: execvp() failed: No such file or directory

Example: Command timeout

#include <iostream>
int main() {
try {
bst::Process process("sleep", {"2"});
process.waitToComplete(); // default timeout is 1000ms
std::cout << e.what() << std::endl;
}
return 0;
}
Definition: exception.hpp:99
const char * what() const noexcept override
Definition: exception.hpp:110
$ ./example
BST Errno: 14 | process timed out