Inside my C code I call system
to have result from a bash command.
Ie 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?
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 (eg 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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.