Ubuntu 16.04安装流程:实现单次重启后执行脚本并自动清理重启
Alright, let's figure out how to solve this for Ubuntu 16.04. You need a one-time trigger that runs your downloaded script on the next reboot, deletes the script afterward, and restarts the system again—all while keeping your existing repeatable startup service intact. Here are two solid approaches:
Since Ubuntu 16.04 uses systemd by default, a oneshot service is the cleanest way to handle this one-time execution requirement. It’ll run exactly once, clean up after itself, and trigger the final restart automatically.
Step-by-Step Setup:
First, make sure your target script (let’s say it’s at
/opt/post-setup-script.sh) has execution permissions:chmod +x /opt/post-setup-script.shCreate a systemd service file at
/etc/systemd/system/one-time-post-reboot.servicewith this content:[Unit] Description=One-time post-reboot setup script execution After=multi-user.target [Service] Type=oneshot ExecStart=/opt/post-setup-script.sh ExecStartPost=/bin/rm -f /opt/post-setup-script.sh ExecStartPost=/bin/systemctl disable --now one-time-post-reboot.service ExecStartPost=/sbin/reboot RemainAfterExit=no [Install] WantedBy=multi-user.targetLet’s break down the key bits:
Type=oneshot: Tells systemd this is a one-and-done task, not a long-running serviceExecStart: Runs your core setup scriptExecStartPostcommands run in order: delete the script, disable/stop this service so it never runs again, then reboot the systemRemainAfterExit=no: Ensures the service doesn’t stay marked as active once it’s done
Enable the service so it triggers on the next reboot:
systemctl enable one-time-post-reboot.service
That’s it. On the next restart, systemd will run the script, clean up, disable itself, and reboot—exactly what you need.
If you prefer the older startup script approach, Ubuntu 16.04 still has rc.local enabled by default. You can use it with a bit of logic to ensure one-time execution.
Step-by-Step Setup:
Again, make sure your script has execution permissions:
chmod +x /opt/post-setup-script.shOpen
/etc/rc.localin your editor, and add this block right before theexit 0line:# One-time post-reboot script execution if [ -f /opt/post-setup-script.sh ]; then # Run the setup script /opt/post-setup-script.sh # Delete the script file rm -f /opt/post-setup-script.sh # Remove this entire block from rc.local to prevent re-runs sed -i '/# One-time post-reboot script execution/,/# Remove this entire block/d' /etc/rc.local # Reboot the system reboot fiHow this works:
- It checks if your script exists before running it
- After execution, it deletes the script and uses
sedto strip this entire logic block fromrc.local—so it can never run again - Finally, it triggers a system restart
Double-check
rc.localhas execution permissions (it should by default, but just in case):chmod +x /etc/rc.local
This method uses the system’s built-in startup mechanism, no extra services required. Just make sure the sed pattern matches exactly to avoid accidentally deleting other parts of rc.local.
- Test either approach in a staging environment first to make sure the script runs, cleans up, and restarts as expected—better safe than sorry!
- Both methods run as root, so no need to add sudo inside your script unless you’re switching users
- Neither approach will interfere with your existing repeatable startup service—they’re completely independent
内容的提问来源于stack exchange,提问作者Tobias

