Several ways to run Python programs in the Linux background

Several ways to run Python programs in the Linux background

1. The first method is to use the unhup command directly to let the program run in the background. The command format is as follows:

unhup python filename.py (> ***.log )&

In this command, python specifies that the file we want to execute is a python file, and the following file name .py is the file we want to execute. The content in brackets indicates that the content normally output to the console can be redirected to the *.log file. This is optional. If this is not done, the content will be output to the nohup.out file by default. The & after the brackets indicates background execution.

2. The second method is to write a script, submit the script to the server, and let the server run the statements in the script in the background. Suppose we define a script start.sh with the following content:

#!/bin/bash
cd the path name of the file you want to run python -u ***.py

In the above script, #!/bin/bash means that this script uses /bin/bash to interpret and execute the following statements, where cd means jumping the current directory to the directory where the file to be run is located, and python -u ***.py means running the ***python file. After writing the script, we can use the following command to execute the script so that the program can run in the background:

./start.sh > result.log &

Here, ./start.sh means running the script start.sh in the current directory, > result.log means outputting everything originally output to the console to the result.log file, and & means running in the background

We can use the ps -e command to view the processes running in the background.


In the above figure, we can see that our script start.sh and Python program have been successfully run in the background. Then we can view the information originally output to the console through cat result.log | more

Note: To execute a function in a Python file, you must remember to call the function in the file in addition to defining the function.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • How to install Redis in Linux, run it in the background, and set up the system to start automatically
  • Several ways of running in the background of Linux (summary)
  • How to view and close background running programs in Linux
  • Linux nohup to run programs in the background and view them (nohup and &)
  • How to start jar package and run it in the background in Linux
  • How to view and terminate running background programs in Linux
  • A brief analysis of the examples and differences of using nohup and screen to run background tasks in Linux
  • How to run springboot in the background of linux
  • Linux background running node service command method
  • Solve the problem of python nohup linux background running output
  • Summary of methods for reliably running processes in the background in Linux
  • Detailed explanation of Linux background operation and shutdown, view background tasks
  • How to run Linux commands in the background

<<:  How to modify the IP restriction conditions of MySQL account

>>:  Comprehensive explanation of CocosCreator hot update

Recommend

8 examples of using killall command to terminate processes in Linux

The Linux command line provides many commands to ...

Is it true that the simpler the web design style, the better?

Original address: http://www.webdesignfromscratch...

Introduction to Linux common hard disk management commands

Table of contents 1. df command 2. du command 3. ...

MySQL FAQ series: When to use temporary tables

Introduction to temporary tables What is a tempor...

How to modify mysql permissions to allow hosts to access

Enable remote access rights for mysql By default,...

Automatically install the Linux system based on cobbler

1. Install components yum install epel-rpm-macros...

Detailed explanation of Linux zabbix agent deployment and configuration methods

1. Install zabbix-agent on web01 Deploy zabbix wa...

HTML+CSS project development experience summary (recommended)

I haven’t updated my blog for several days. I jus...

The shortest JS to determine whether it is IE6 (IE writing method)

Commonly used JavaScript code to detect which ver...

Web front-end development course What are the web front-end development tools

With the development of Internet technology, user...

HTML Web Page List Tags Learning Tutorial

HTML web page list tag learning tutorial. In HTML ...

React Hooks Detailed Explanation

Table of contents What are hooks? Class Component...

Tutorial on disabling and enabling triggers in MySQL [Recommended]

When using MYSQL, triggers are often used, but so...