Search code examples
linuxdebianstartupsystemd

Using systemd, how can I identify cold boot vs reboot in a service?


I am trying to execute a systemd service on Debian. However, I need the logic to vary based on whether it is booting up 'cold', or from a restart.

I currently have a run-application.service which looks like this:

[Unit]
Description=Run application

[Service]
Type=oneshot
ExecStart=/usr/bin/run-application

[Install]
WantedBy=graphical.target

I want the program to be called on every boot, but the internal logic needs to be modified in the case of a reboot. What is the easiest/best way of achieving this?


Solution

  • General Foch is right. Create a new systemd service, which is wanted by reboot.target, and leaves a 'mark' somewhere on the filesystem, which can get picked up on reboot. I wouldn't recommend putting it in /tmp, as the cleaning of /tmp is done by a upstart script everytime /tmp is mounted - practically, that means at every boot. I would put it in a directory that you know will persist. That way, you can know if you have come from a reboot, and you can reasonably assume that any other time you are booting that it is 'cold'. This will account for situations such as the machine losing power, etc.