systemd - stop system init if condition

systemd - stop system init if condition

I would like to run a script after systemd loads some targets and halt/reboot the system after checking a condition.

My systemd service unit in /etc/systemd/system looks like this:

[Unit]
Description=My Service
After=local-fs.target
Before=basic.target

[Service]
ExecStart=/path/to/my-script

[Install]
WantedBy=local-fs.target

Inside my-script I have:

echo "script run $(date)" >> /home/kelvin/test.log

if [ condition ]; then
        reboot
        exit 0 
fi

exit 0

The scripts works if I run sudo systemctl start my-service, but it wont work on system startup.

相关内容