在Django中使用Huey:如何保持run_huey命令持续运行?
Great question—running background tasks reliably on Ubuntu 18.04 is a common need, and there are a few solid approaches depending on your use case. Let's break this down into two parts: keeping run_huey alive after SSH disconnects, and making it start automatically on system boot.
run_huey Running After SSH Disconnects 1. Quick Fix: Use nohup
If you need a simple, one-off solution, nohup (short for "no hangup") is perfect. It detaches the process from your SSH session and redirects output to a log file so it keeps running even after you disconnect.
Run this command in your Django project directory:
nohup python manage.py run_huey > huey.log 2>&1 &
> huey.log 2>&1redirects both standard output and errors tohuey.log(you can change the filename if you want).- The final
&sends the process to the background.
To check if it's running:
ps aux | grep run_huey
To stop the process later, use kill <PID> where <PID> is the process ID from the above command.
2. Interactive Control: Use screen or tmux
If you want to be able to reconnect to the process and monitor its output later, use a terminal multiplexer like screen or tmux.
Using screen:
- Install it first (if not already installed):
sudo apt update && sudo apt install screen - Start a new screen session named "huey":
screen -S huey - Run your
run_hueycommand as usual:python manage.py run_huey - Detach from the session without stopping it: press
Ctrl + A, thenD. - Later, to reconnect to the session:
screen -r huey - To stop the session entirely, reconnect and press
Ctrl + D, or kill the process withkill <PID>.
3. Production-Grade: Use systemd
For the most reliable setup (especially if you need the process to restart automatically if it crashes), use systemd—Ubuntu 18.04's default init system. This also solves your auto-start-on-reboot need in one go.
Step 1: Create a systemd service file
Open a new service file with nano:
sudo nano /etc/systemd/system/huey.service
Paste the following configuration, replacing placeholders with your actual details:
[Unit] Description=Huey Task Runner for Django Application After=network.target # Wait for network to be ready before starting [Service] User=your_username # Replace with your Ubuntu username WorkingDirectory=/path/to/your/django/project # Full path to your Django project root ExecStart=/path/to/your/python/environment/bin/python manage.py run_huey # Path to your Python executable (use virtualenv path if you're using one) Restart=always # Restart the service if it crashes RestartSec=5 # Wait 5 seconds before restarting StandardOutput=journal+console # Send logs to systemd journal StandardError=journal+console [Install] WantedBy=multi-user.target # Start when the system reaches multi-user mode
Save and exit nano: press Ctrl + O, then Enter, then Ctrl + X.
Step 2: Enable and start the service
- Reload
systemdto recognize the new service:sudo systemctl daemon-reload - Start the Huey service:
sudo systemctl start huey - Verify it's running:
systemctl status huey
run_huey on System Reboot If you used the systemd method above, enabling auto-start is just one more command:
sudo systemctl enable huey
This tells systemd to start the Huey service every time the system boots up.
If you're not using systemd (not recommended):
You can use crontab to run the nohup command on reboot, but this is less reliable than systemd (it won't restart the process if it crashes).
Edit your crontab:
crontab -e
Add this line at the end (replace paths as needed):
@reboot nohup /path/to/python /path/to/manage.py run_huey > /path/to/huey.log 2>&1 &
Save and exit, and the command will run on every system restart.
内容的提问来源于stack exchange,提问作者Valachio

