Reputation: 3607
Inside my C code I call system
to have result from a bash command.
I.e. system("ps aux | grep my_prog | head -n 1")
All is fine when I run my program in foreground mode, but in production is a service, so I need to see the output of system
in syslog
, not on stdout
or stderr
.
I'm struggling to find the best and least painful option here. Is there a proper way to do it?
Upvotes: 1
Views: 298
Reputation: 72639
The proper way is to use popen()
instead of system()
and then the syslog interface, man 3 syslog
:
NAME
closelog, openlog, syslog - send messages to the system logger
SYNOPSIS
#include <syslog.h>
void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);
#include <stdarg.h>
void vsyslog(int priority, const char *format, va_list ap);
Also note that grepping ps
output has its pitfalls (e.g. it may also return the grep
process first). Why not search for the proper process in the C code iterating over ps
output? Or directly iterate over the process table using functions your OS provides?
Upvotes: 3