Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

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, 2024 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().) Commented Jun 24, 2024 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, 2024 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, 2024 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. Commented Jun 26, 2024 at 9:20