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

在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.

Keeping 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>&1 redirects both standard output and errors to huey.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:

  1. Install it first (if not already installed):
    sudo apt update && sudo apt install screen
    
  2. Start a new screen session named "huey":
    screen -S huey
    
  3. Run your run_huey command as usual:
    python manage.py run_huey
    
  4. Detach from the session without stopping it: press Ctrl + A, then D.
  5. Later, to reconnect to the session:
    screen -r huey
    
  6. To stop the session entirely, reconnect and press Ctrl + D, or kill the process with kill <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

  1. Reload systemd to recognize the new service:
    sudo systemctl daemon-reload
    
  2. Start the Huey service:
    sudo systemctl start huey
    
  3. Verify it's running:
    systemctl status huey
    
Automatically Starting 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.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 13:39:07