19

In Linux, RHEL-8.10 or later if it matters, I would like to write some C code that takes in various parameters and upon a certain condition issues a system("init 0") to shutdown the system. Before issuing the shutdown, I would like to throw a one line message into /var/log/messages that states system being shut down by ron's code so that I have a record of when that has happened.

How do you properly write into /var/log/messages?

Obviously I can do an fopen("/var/log/messages, "a") and a simple fprintf but that seems like a bit of a hack.

11
  • 4
    Do you need to write your own C code or would using the standard logger utility from a shell script match your requirements better? Commented Jun 24 at 11:18
  • 2
    Why does fopen() + fprintf() "feel like a hack"? It's pretty much the standard C way to write to a a particular file, if that's what you want to do. (You could also use the lower level POSIX interfaces open() and write().)
    – ilkkachu
    Commented Jun 24 at 20:53
  • 5
    @ilkkachu for /var/log/messages it’s not ideal though — in particular, the process might not have permission to write to it, whereas the system logger would. Commented Jun 24 at 21:10
  • 7
    Nitpick: Never call init 0 on a modern Linux system, it’s a legacy interface if you’re using systemd (and is thus liable to go away whenever the systemd developers feel like it shouldn’t exist anymore) and it almost always disconnects any other login sessions without warning. Always prefer shutdown -hP now, or on systemd systems possibly systemctl poweroff. They’re not legacy interfaces and will ensure that logged in users actually get notified that the system is shutting down (and will also log the shutdown, though not to the degree you seem to want to). Commented Jun 25 at 1:43
  • 2
    @AndrewHenle, I would expect a single a fprintf() to result in just one write(), actually. But you're right, it might not, so manually doing sprintf() and write() might be better. Remembering to open in append mode is important, though, but they did have "a" there already. The issue with this question is that they're not really saying they want to send a message to the regular system logging infrastructure, but that they just want to write to a particular file. Those are two different questions, with two different answers, and syslog() is only an answer to one of them.
    – ilkkachu
    Commented Jun 26 at 9:20

1 Answer 1

45

You should send your log messages to the system logger, using functions declared in syslog.h.

In your initialisation code, add

openlog("ronboot", 0, LOG_USER);

(replace ronboot with an appropriate identifier; if you specify NULL, the program’s name will be used automatically).

When you want to log something, use

syslog(LOG_INFO, "system being shut down");

In your cleanup code, add

closelog();

The system logger will write the message to the appropriate log file. That depends on the facility and log level used; LOG_USER / LOG_INFO messages typically go to /var/log/messages.

2
  • 12
    In more detail, this sends the message to the syslog daemon, if one is running. On a lot of current distributions that's systemd-journald; on a lot of older ones it's syslogd or something like rsyslog. On some systems it might not go anywhere, but then none of the log messages are going anywhere, and that's the admin's problem, not yours. Commented Jun 24 at 10:41
  • @StackExchangeSupportsIsrael Ubuntu, for example, uses both journald and rsyslog
    – OrangeDog
    Commented Jun 27 at 9:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .