Reputation: 23
I'm trying to do this in my .cpp file:
system("/home/user/workspace/script.sh");
I just want to open it, get it running on its shell and then forget about it. My solution so far is just doing this:
pid_t pid = fork();
if (pid == 0) {
system("/home/user/workspace/script.sh");
}
And it works. The thing is that I'm not sure if that is properly done, but even if it is, I am getting some warnings that I don't want to see in my output:
(gnome-terminal:8105): GLib-GIO-CRITICAL **: g_settings_get: the format string may not contain '&' (key 'monospace-font-name' from schema 'org.gnome.desktop.interface'). This call will probably stop working with a future version of glib.
(gnome-terminal:8105): Vte-2.90-WARNING **: No se pueden convertir caracteres de UTF-8 a actual.
(gnome-terminal:8105): Vte-2.90-WARNING **: No se pueden convertir caracteres de UTF-8 a actual.
(gnome-terminal:8105): Vte-2.90-WARNING **: No se pueden convertir caracteres de UTF-8 a actual.
Unhandled value type TerminalEncoding of pspec encoding
Any advices? Thank you so much.
Upvotes: 0
Views: 205
Reputation: 1901
The system()
function causes running a separate process using the current shell (this is what interprets the string argument), and this process behaves exactly as if you ran it from the shell manually (that is, it will spit its stdout and stderr streams as usual). You can do two things:
system()
(as hinted in the comment)fork()
, then "make the process switch itself into a command" using execl()
. Between fork()
and execl()
you can close()
unwanted streams (1 = stdout, 2 = stderr), and if this caused problems in running the secondary process when it tried to write to a closed file, you can dup()
to them a newly open()
-ed descriptor to a "/dev/null" fileActually the second solution is the same as the first one in the final result - the difference is that second one does not involve shell to execute your process, but it's more complicated.
Upvotes: 1