PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 17, 2022

[FIXED] How to redirect the output of system() called in a C program to syslog?

 August 17, 2022     c, output, syslog, system     No comments   

Issue

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?


Solution

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?



Answered By - Jens
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing