Hi Everyone,

I have a systemd timer that fires early in the morning. The timer
starts a systemd service, and the service checks for updates using the
package manager. If updates are found then they are applied. The
service runs fine and is shown below.

The tail of the service schedules a reboot of the machine at +10
minutes when updates are applied. The reboot clears a UI widget that
confuses my users and loads the latest components upon reboot.

The reboot is not happening. When I inspected the systemd logs I see
that it was supposed to happen. When I check the user's desktop that
damn useless UI widget is present nagging about installing updates.

How do I schedule a reboot using systemd service file?

Thanks in advance.

=========================

$ cat /etc/systemd/system/system-update.service
[Unit]
Description=Update the system once a day without user prompts

[Service]
Type=oneshot
ExecStart=/usr/sbin/system-update
Wants=system-update.timer

[Install]
WantedBy=system-update.target

=========================

$ cat /usr/sbin/system-update
#!/usr/bin/env bash

PATH=/sbin:/usr/sbin:/bin:/usr/bin

# Update the package lists
if apt-get update &>/dev/null
then
    echo "Updated package list"
else
    echo "Failed to update package list"
    [[ "$0" = "${BASH_SOURCE[0]}" ]] && exit 1 || return 1
fi

# If no packages are upgradeable, then the message is "Listing... Done".
# Otherwise a package name is listed as upgradeable.
COUNT=$(apt list --upgradable 2>/dev/null | grep -v 'Listing' | wc -l)

# Only update and reboot if packages are available
if [[ "$COUNT" -gt 0 ]]
then
    if apt-get dist-upgrade -y &>/dev/null
    then
        echo "Upgraded system"
    else
        echo "Failed to upgrade system"
        [[ "$0" = "${BASH_SOURCE[0]}" ]] && exit 1 || return 1
    fi

    echo "Purging old packages"
    apt autoremove --purge &>/dev/null

    NEEDS_REBOOT=1
fi

if [[ -f /var/run/reboot-required ]]
then
    NEEDS_REBOOT=1
fi

if [[ "$NEEDS_REBOOT" -eq 1 ]]
then
    echo "Scheduling reboot in 10 minutes"
    reboot -r 10
fi

[[ "$0" = "${BASH_SOURCE[0]}" ]] && exit 0 || return 0

=========================

$ systemctl status system-update.service
? system-update.service - Update the system once a day without user prompts
   Loaded: loaded (/etc/systemd/system/system-update.service; enabled; vendor pr
   Active: inactive (dead) since Wed 2019-05-15 05:03:59 EDT; 10h ago
  Process: 17218 ExecStart=/usr/sbin/system-update (code=exited, status=0/SUCCES
 Main PID: 17218 (code=exited, status=0/SUCCESS)

May 15 05:01:28 qotom systemd[1]: Starting Update the system once a day without
May 15 05:01:34 qotom system-update[17218]: Updated package list
May 15 05:03:58 qotom system-update[17218]: Upgraded system
May 15 05:03:58 qotom system-update[17218]: Purging old packages
May 15 05:03:59 qotom system-update[17218]: Scheduling reboot in 10 minutes
May 15 05:03:59 qotom systemd[1]: Started Update the system once a day without u
_______________________________________________
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel

Reply via email to