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.
loggerutility from a shell script match your requirements better?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 interfacesopen()andwrite().)/var/log/messagesit’s not ideal though — in particular, the process might not have permission to write to it, whereas the system logger would.init 0on 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 prefershutdown -hP now, or on systemd systems possiblysystemctl 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).fprintf()to result in just onewrite(), actually. But you're right, it might not, so manually doingsprintf()andwrite()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, andsyslog()is only an answer to one of them.