Windows Tip: Run applications in the background using Task Scheduler
I was working on a project a couple of weeks ago which involves Celery for processing tasks. I wanted the Celery process to run in the background as a service but it didn’t come with a Windows service installer, we will have to write our own. Since we were still just working on a proof of concept, I didn’t want to spend too much time on this at this point and then I realized I could probably just use the built in Windows Task Scheduler application.
The reason I wanted Celery to run in the background as a service was because one of the worker nodes we’re using is shared with another group. They may need to reboot the machine at any time or log off my user session which would kill the Celery process. I wanted the Celery process to automatically start when Windows starts, run silently in the background using a user account I specify, automatically restart the task on failure, and manage it remotely without fully logging in to the machine where it runs.
I was able to accomplish all of the above by simply using Task Scheduler. Below are the settings I used.
Notes
The content of celerystart.bat script is basically just the command to call and start the celeryd process.
For example:
python -m celeryd.bin.celeryd
or if you don’t have the environment variable set for the Python executable:
C:\Python27\python -m celery.bin.celeryd
You can specify additional options in the command as well such as the log file location, log level, number of workers, etc. See the celery docs for a list of all the available options.