0

Hey faultless server folks,

I'd like to write a systemd service that can update itself.

Specifically, every 5 minutes it should look at Amazon S3 for new versions of its binary and .service files. If they've been updated, it should download and replace them, and then restart itself.

Unfortunately, I understand systemd doesn't allow modification of in-use files.

Is there some standard way to achieve this?

Thanks!

EDIT: To clarify, I'm asking if it's possible to do this with a single service. I realize I could have a pair of services which update each other, but that seems inelegant.

1 Answers1

1

Updating files related to systemd .service files or program executables is the same as updating any other file.

You should really do this with configuration management such as Ansible, but to get it done as-is your general workflow could be using a bash/python script that does the following:

  1. Gets the file(s) from the web server/s3/etc.
    • You can do a quick md5sum check to compare differences. Or compare dates, other metadata to determine if the file is new or changed
  2. Replace the files as needed
    • You may need to stop the service (service <servicename> stop) beforehand if the files are in-use.
  3. Run systemctl daemon-reload to tell systemd to "re-scan" the service files for any changes.
  4. Restarts/starts the service in question

You can then load that script up in cron or as a systemd timer to run on a schedule.

Rino Bino
  • 511
  • 5
  • 21
  • 1
    Thanks, and I take it to mean the answer is "no". No, a service can't update itself - you need to use a second service to update the first one. – emchristiansen Apr 28 '22 at 01:48
  • Well, I think it leans more towards "yes - it's technically possible". It depends on what process is running as the service. If you ran the above script process as a systemd service you could do it. Like a self-updater mechanism written into the process itself. It could even restart itself. Depends on the use-case but most things are always possible depending on how creative you get. – Rino Bino Apr 28 '22 at 02:00
  • If your service executes a binary at a path, and your 'updater' service finds the new binary, pulls it down (replaces the old with a symlink to the new), and restarts the service, you woudln't even need a deamon-reload if you're not actually updating the *.service file – Cinderhaze Dec 06 '22 at 06:27