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

服务器重启后自动重启Spyder中运行的Python常驻代码的方法咨询

Great question! Running long-lived Python scripts on servers that might reboot unexpectedly is a super common pain point, and there are a few solid, reliable ways to fix this. Let’s break down the best options, ordered by how robust they are:

This is the most reliable method—systemd is built into most modern Linux distros, and it’ll not only restart your script on server reboot but also if the script crashes unexpectedly.

Here’s how to set it up:

  • First, make sure your script can run independently of Spyder. Test it with a direct Python command:

    python3 /full/path/to/your/script.py
    

    Pro tip: Update all file paths in your script to absolute paths (like /home/your-user/data/file.csv instead of data/file.csv)—systemd runs scripts in a different working directory than your Spyder session.

  • Create a systemd service file. Open a terminal and run:

    sudo nano /etc/systemd/system/your-script.service
    

    Paste this template, replacing placeholders with your details:

    [Unit]
    Description=My Long-Running Python Script
    After=network.target  # Add this if your script needs internet access
    
    [Service]
    User=your-username  # Replace with your server username
    ExecStart=/usr/bin/python3 /full/path/to/your/script.py  # Path to Python and your script
    Restart=always  # Restart the script if it crashes OR the server reboots
    RestartSec=5  # Wait 5 seconds before restarting
    WorkingDirectory=/full/path/to/your/script/folder  # Optional: Set the script's working directory
    
    [Install]
    WantedBy=multi-user.target
    

    Save and exit (Ctrl+O, Enter, then Ctrl+X in nano).

  • Enable and start the service:

    sudo systemctl daemon-reload
    sudo systemctl enable your-script.service  # Makes it start on boot
    sudo systemctl start your-script.service   # Starts it right now
    

    You can check its status anytime with:

    systemctl status your-script.service
    

    And view logs with:

    journalctl -u your-script.service -f
    
2. Use crontab’s @reboot Trigger (Quick & Simple)

If you want a lighter-weight solution without systemd’s monitoring features, crontab can run your script automatically on reboot.

Steps:

  • Again, ensure your script runs correctly with a direct Python command (absolute paths are key here too).

  • Create a shell script to launch your code (optional but helps with delays/logging). Make a file like start-my-script.sh in your home folder:

    #!/bin/bash
    sleep 60  # Give the server time to load network/disk services first
    /usr/bin/python3 /full/path/to/your/script.py >> /home/your-username/script-logs.log 2>&1
    

    Make it executable:

    chmod +x ~/start-my-script.sh
    
  • Edit your crontab:

    crontab -e
    

    Add this line at the bottom:

    @reboot /home/your-username/start-my-script.sh
    

    Save and exit. Now your script will run every time the server boots up.

If you really need to run the script inside Spyder (e.g., for GUI debugging), this only works if your server has a desktop environment and auto-login enabled (most servers don’t use desktops, so skip this unless necessary):

  • Open your desktop’s "Startup Applications" tool (location varies by DE—GNOME, KDE, etc.).
  • Add a new entry with the command:
    spyder --script /full/path/to/your/script.py
    
    This will launch Spyder and run your script automatically when you log into the desktop. Note: It won’t run if no one logs in after a reboot.

内容的提问来源于stack exchange,提问作者Data_Science_110

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 16:07:35