0

I have a Debian based OS, and I am creating a program that have support for upgrading itself. The program is ran on system-level as a systemd service.

I have a problem when I update the software, which in practice means replacing some files on /opt/myApp/ folder (home of all my application code) and replacing systemd service unit file /etc/systemd/system/my_service.service. After this I run systemctl daemon-reload, systemctl enable my_service and systemctl restart which works as intended. How ever, if a sudden power loss happens right after this, the new files copied to /opt/myApp/ and the new service file goes to size 0 in the filesystem, which of course then causes my program to fail and the service to go into masked state. If I reboot my system after the installation, the "danger" goes away and the system can handle sudden power losses without effect on the files I care about.

Is this some kind of file system corruption problem? How could I prevent this problem without rebooting the system? What could be the root of this issue?

ml_kalju
  • 3
  • 3
  • Call `sync` to write buffers to disk? – vidarlo Jul 11 '23 at 07:42
  • @vidarlo As a pretty new linux developer I didn't even know that files created doesn't immediately go to persistent storage. Thanks, that worked. I think there is no way to make comment a solve to a question? – ml_kalju Jul 11 '23 at 07:58
  • All modern operating systems have write buffers. – vidarlo Jul 11 '23 at 08:24

1 Answers1

3

Consider .deb packaging your software. This way, dpkg will handle this issue as well as the entire update process including the application files & the SystemD service unit. If something goes wrong, e.g., a sudden power loss or other interruption, dpkg will be aware of the situation and suggest the necessary action, which might be a re-installation of the package or dpkg --configure -a.

Esa Jokinen
  • 46,944
  • 3
  • 83
  • 129