Score:0

Is it possible for a systemd service to update itself?

in flag

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.

4snok avatar
es flag
why don't you want to use systemd.times or cron for this task?
Score:1
us flag

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.

Cinderhaze avatar
us flag
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
in flag
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.
Rino Bino avatar
us flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.