服务器重启后自动重启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.pyPro tip: Update all file paths in your script to absolute paths (like
/home/your-user/data/file.csvinstead ofdata/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.servicePaste 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.targetSave 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 nowYou can check its status anytime with:
systemctl status your-script.serviceAnd view logs with:
journalctl -u your-script.service -f
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.shin 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>&1Make it executable:
chmod +x ~/start-my-script.shEdit your crontab:
crontab -eAdd this line at the bottom:
@reboot /home/your-username/start-my-script.shSave 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:
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.spyder --script /full/path/to/your/script.py
内容的提问来源于stack exchange,提问作者Data_Science_110

