You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

方案1:使用Systemd One-Shot服务(推荐)

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:

  1. 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.sh
    
  2. Create a systemd service file at /etc/systemd/system/one-time-post-reboot.service with 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.target
    

    Let’s break down the key bits:

    • Type=oneshot: Tells systemd this is a one-and-done task, not a long-running service
    • ExecStart: Runs your core setup script
    • ExecStartPost commands run in order: delete the script, disable/stop this service so it never runs again, then reboot the system
    • RemainAfterExit=no: Ensures the service doesn’t stay marked as active once it’s done
  3. 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.

方案2:使用rc.local(传统兼容方式)

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:

  1. Again, make sure your script has execution permissions:

    chmod +x /opt/post-setup-script.sh
    
  2. Open /etc/rc.local in your editor, and add this block right before the exit 0 line:

    # 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
    fi
    

    How this works:

    • It checks if your script exists before running it
    • After execution, it deletes the script and uses sed to strip this entire logic block from rc.local—so it can never run again
    • Finally, it triggers a system restart
  3. Double-check rc.local has 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.

Quick Notes:
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:29:28