How to kill background job processes

Is there any way to kill specific background job process? I’ve read about bench purge-jobs, but I need to destroy not only the pending ones, but also the process that currently running.

The reason I needed this is because there was an error during background job process that shown in logs/worker.log the process just stopped there, and I could not do anything except restart the server (reboot the machine).

From console:

frappe.get_doc("RQ Job", "your_site::scheduled_job::your_scheduled_job_name").stop_job()

Hope this helps.

1 Like

thanks for the answer,

tried that, but got this error.

Module import failed for RQ Job (frappe.core.doctype.rq_job.rq_job Error: No module named 'frappe.core.doctype.rq_job')

I’m currently in frappe v13 if that a problem.

I think that RQ Jobs new handler was included only in v14, not backported.
Probably using Redis CLI you can stop jobs with job_id.

Hope this helps.

1 Like

Yes it is possible to cancel running background job through RQ jobs. but it needs to done manually from console for v13. thanks

from rq.command import send_stop_job_command
from rq import Worker
from frappe.utils.background_jobs import get_redis_conn
con = get_redis_conn()
workers = Worker.all(con)
for w in workers:
    job = w.get_current_job()
    if job:
        if job.id == "your_job_id":
            send_stop_job_command(con, job)
1 Like

bench purge-jobs
run this command
it will remove all the pending jobs