- How to Run a Long-Running Background Task in Python
- Need to Run a Long-Running Background Task
- How to Run a Long-Running Background Task
- Run Background Process in Python
- Checking Running Processes and Services
- Running background processes in Python
- Step 1: Create a Python file with the code we are interested in running in the background
- Step 2: Create another Python script that will wake up the Python process in the background
- subprocess package:
- Step 3: Execute the run_process.py file on the code editor
- Killing a running process
- Using Task Manager:
- On the command prompt:
- In Python
- Conclusion
- Как запустить python скрипт в фоновом режиме?
- Running a Python Script in the Background
- Make Python Script Executable
- Start Python Script in Background
- Find and Kill the Running Process
- Output Buffering
How to Run a Long-Running Background Task in Python
You can run a long-running monitoring task using a daemon thread and a while loop.
In this tutorial you will discover how to execute long-running tasks in the background in Python.
Need to Run a Long-Running Background Task
A thread is a thread of execution in a computer program.
Every Python program has at least one thread of execution called the main thread. Both processes and threads are created and managed by the underlying operating system.
Sometimes we may need to create additional threads in our program in order to execute code concurrently.
Python provides the ability to create and manage new threads via the threading module and the threading.Thread class.
You can learn more about Python threads in the guide:
In concurrent programming, we may need to run a long-running task in the background.
This may be for many reasons, such as:
- Monitor an external resource for change.
- Monitor program state for specific conditions.
- Monitor data for negative or favorable values.
Here, long-running means for the duration of the main program.
Background means that the task is not the main purpose of the application, but instead is a task that supports the main application.
How can we run long-running tasks in the background in Python?
Run your loops using all CPUs, download my FREE book to learn how.
How to Run a Long-Running Background Task
You can run a long-running task in the background using a daemon thread.
A daemon thread is a background thread. Daemon is pronounced “dee-mon“, like the alternate spelling “demon“.
A thread may be configured to be a daemon or not, and most threads in concurrent programming, including the main thread, are non-daemon threads (not background threads) by default. The difference between daemon threads and non-daemon threads is that the process will exit if only daemon threads are running, whereas it cannot exit if at least one non-daemon thread is running.
As such, daemon threads are helpful for executing tasks in the background to support the non-daemon threads in an application.
If you are new to daemon threads, you can learn more about them here:
A Python threading.Thread instance can be configured to be a daemon thread.
We can configure a new thread to be a daemon thread by specifying the “daemon” argument to True in the constructor of the threading.Thread class.
Run Background Process in Python
Background processes are computer processes that run behind the scenes. The core advantage of running processes in the background is that you can do other tasks in the foreground. In this article, we will discuss how to run background processes using Python. We will also discuss how to check and stop running processes/applications.
Checking Running Processes and Services
To check the running processes in Windows, you can use the Task Manager App or run the command “tasklist” on the command line.
Some processes or services start when the computer is started, while the user explicitly starts others. A process can also create another process. For example, a Python process is begun when running a Python script on an editor or an IDE like Sublime Text or Visual Studio. The rest of the processes running under Sublime Text in the Task Manager below are just processes supporting the two main processes.
The problem is that once Sublime Text is closed, all these processes are closed – including the program being executed in Python. This is where understanding how to run the background process comes in.
Running background processes in Python
In Python, any of the two packages, namely, subprocess and os, can be used to run background processes. The general syntax is:
To demonstrate how these packages work in running a background process, let’s run a Python script. Even if the code editor is closed, the python.exe process will keep running in the background until the execution is complete. This is because the Python process is not attached to the process holding the editor alive.
Step 1: Create a Python file with the code we are interested in running in the background
Create a python script “process.py” with the following code in it:
The code should run for around 100 seconds and write a text file (results.txt) into the disk with numbers 0 through 4 written in it. If you execute this file on the editor directly and accidentally or otherwise close the editor, the execution process is also terminated. This is because closing the editor also closes the Python process, which is responsible for executing the code.
Step 2: Create another Python script that will wake up the Python process in the background
To ensure that the code on “process.py” is executed in the background until completion, let us create another file Python script called “run_process.py” and use the two packages, subprocess, and os, to run the code in “process.py” in the background.
The file “run_process.py” contains the following lines of codes based on the choice of the package to use:
subprocess package:
and, for the os package, we have
Step 3: Execute the run_process.py file on the code editor
Note that a Python background process running “process.py” is initiated when you execute “run_process.py” on the code editor. This process remains awake until the execution of the “process.py” script is completed, even if you close the editor.
This process locks the results.txt file, so if you try to delete it during the program operation, you will be greeted with the message that the file is in use, so you can either wait or kill the process.
Killing a running process
We need the Image Name or the Process ID (PID) to kill a running process. This information can be found on the Task Manager or by running the tasklist on the command line. You might need to right-click on the title section and select PID and Process name on Task Manager (see below).
Any of the following options can be used to terminate a running process (refer to the above screenshot of Task Manager for the following points).
Using Task Manager:
select the process, right-click, and click “end task” at the bottom right of the manager window.
On the command prompt:
- when using PID run “taskkill /PID /PID /F“, for example, “taskkill /PID 11024 /F” to kill CTF Loader.
- When using Image Name, run “taskkill /IM /IM /F” for example “taskkill /IM ctfmon.exe /F” to kill CTF Loader.
In Python
You can kill running tasks using subprocess or os as follows (note that you need to import these packages):
Note: The /F option issued to the taskkill command is optional. It is used to terminate a process forcefully. This option might be needed when closing a task used by another process, for example.
The PID changes every time you open the process/app. This means that the PID used to kill a task generated by a given app may not work when an app is closed and opened again. In such a case, Image Name is a better option to use.
Conclusion
In this article, we discussed how to wake up a process and keep running in the background using Python. We have demonstrated how this is done by running a python.exe process in the background (you can do this with any process). Lastly, we have shown how to terminate a running process(es) using Task Manager, command prompt, and Python packages.
Как запустить python скрипт в фоновом режиме?
Я тут работаю над одним проектом(личный бот по факту) и возникла задача запустить его в фоне. То есть процесс будет висеть , но консоль(командная строка) будет закрыта(не свернута). Запуск скрипта с добавлением в конец & — не дает нужного результата. Думаю что нужно добавить пару строчек кода в сам скрипт.
Подскажите кто знает
Можно написать демон для systemd если в вашей операционной системе он используется.
Создаём файл демона:
sudo touch /etc/systemd/system/bot.service
[Unit] Description=My bot After=multi-user.target [Service] Type=idle ExecStart=/usr/bin/python /путь/до/скрипта/bot.py Restart=always [Install] WantedBy=multi-user.target
После этого в консоли выполяем:
sudo systemctl daemon-reload sudo systemctl enable bot.service sudo systemctl start bot.service
Чтобы остановить бот:
sudo systemctl stop bot.service
Чтобы удалить из автозагрузки:
sudo systemctl disable bot.service
Чтобы проверить работу демона:
sudo systemctl status bot.service
yahabrovec, тю. С этого и нужно было начинать 🙂
Как это делается в винде я не знаю.
Автору ответа большое спасибо, ответ помог настроить скрипт.
Я же добавлю свою настройку для запуска скрипта из окружения
Суть: у меня проект юзает pipenv и надо было его запускать из окружения. В целом не суть важно через что у вас создается окружение, настроить запуск скрипта из окружения возможно следующими шагами.
1) залейте проект на сервак (/home/myuser/tel-bot)
2) ставьте окружение, узнайте полный путь к питоноу вашего окружения (/home/myuser/.local/share/virtualenvs/tel-bot-31zbdxgR/bin/python)
3) запускаем скрипт из окружения: ExecStart=/bin/bash -c ‘cd /home/myuser/tel-bot && /home/myuser/.local/share/virtualenvs/tel-bot-31zbdxgR/bin/python run.py’
Вот полный конфиг systemd:
[Unit] Description=super puper bot After=multi-user.target [Service] Type=idle ExecStart=/bin/bash -c 'cd /home/myuser/tel-bot && /home/myuser/.local/share/virtualenvs/tel-bot-31zbdxgR/bin/python run.py' Restart=always [Install] WantedBy=multi-user.target
Дмитрий, а если сперва надо перейти в каталог питон проекта а после запустить main.py как будет выглядеть команда?
ExecStart=/bin/bash -c ‘cd /home/myuser/scrypt && /usr/bin/python3 main.py’
ExecStart=/bin/bash -c 'cd /home/myuser/tel-bot && /home/myuser/.local/share/virtualenvs/tel-bot-31zbdxgR/bin/python run.py'
то при остановке или перезагрузки службы, у меня скрипт не отрабатывал до конца (как это было при завершении обычной работы скрипта [ctrl + C])
Первую команду из ExecStart можно убрать. Просто в той же секции [Service] пишем:
WorkingDirectory=/home/myuser/tel-bot
В итоге скрипт норм завершается, получаем:
WorkingDirectory=/home/myuser/tel-bot ExecStart=/bin/bash -c '/home/myuser/.local/share/virtualenvs/tel-bot-31zbdxgR/bin/python run.py'
Running a Python Script in the Background
This is a quick little guide on how to run a Python script in the background in Linux.
Make Python Script Executable
First, you need to add a shebang line in the Python script which looks like the following:
This path is necessary if you have multiple versions of Python installed and /usr/bin/env will ensure that the first Python interpreter in your $PATH environment variable is taken. You can also hardcode the path of your Python interpreter (e.g. #!/usr/bin/python3 ), but this is not flexible and not portable on other machines. Next, you’ll need to set the permissions of the file to allow execution:
Start Python Script in Background
Now you can run the script with nohup which ignores the hangup signal. This means that you can close the terminal without stopping the execution. Also, don’t forget to add & so the script runs in the background:
If you did not add a shebang to the file you can instead run the script with this command:
nohup python /path/to/test.py &
The output will be saved in the nohup.out file, unless you specify the output file like here:
nohup /path/to/test.py > output.log & nohup python /path/to/test.py > output.log &
Find and Kill the Running Process
You can find the process and its process Id with this command:
If you want to stop the execution, you can kill it with the kill command:
It is also possible to kill the process by using pkill, but make sure you check if there is not a different script running with the same name:
Output Buffering
If you check the output file nohup.out during execution you might notice that the outputs are not written into this file until the execution is finished. This happens because of output buffering. If you add the -u flag you can avoid output buffering like this:
Or by specifying a log file:
nohup python -u ./test.py > output.log &