Skip to content

Instantly share code, notes, and snippets.

@b23prodtm
Last active October 30, 2019 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b23prodtm/a0cebe97817800e10764928a93ef56ab to your computer and use it in GitHub Desktop.
Save b23prodtm/a0cebe97817800e10764928a93ef56ab to your computer and use it in GitHub Desktop.
Check if the system dpkg need a restart and send corresponding signal, as a service
#!/bin/bash
# auto-reboot
# description: Check if the system dpkg need a restart and send corresponding signal
start() {
# code to start app comes here
# example: bahs -c "program_name" &
bash -c "while [ ! -f /var/run/reboot-required ]; do sleep 30; done; cat /var/run/reboot-required.dpkgs 2> /dev/null; reboot" &
echo $! > /var/run/chkdpkgs.pid
}
stop() {
# code to stop app comes here
# example: kill program_name.pid
kill $(cat /var/run/chkdpkgs.pid)
rm /var/run/chkdpkgs.pid
}
status() {
if [ -e /var/run/chkdpkgs.pid ]; then
echo auto-reboot is running, pid=$(cat /var/run/chkdpkgs.pid)
else
echo auto-reboot is NOT running
exit 1
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
# code to check status of app comes here
# example: status program_name
status
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac
exit 0
@b23prodtm
Copy link
Author

/etc/systemd/system/auto-reboot.service
`[Unit]
Description=Auto-Check system reboot-required status and reboot

[Service]
Type=forking
ExecStart=/usr/local/bin/auto-reboot.sh start
ExecStop=/usr/local/bin/auto-reboot.sh stop
PIDFile=/var/run/chkdpkgs.pid

[Install]
WantedBy=multi-user.target
`

@b23prodtm
Copy link
Author

sudo systemctl enable auto-reboot

@b23prodtm
Copy link
Author

The system should reboot at anytime if the message **** System reboot is required **** is displayed on login shell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment