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

Use native js to simulate the scrolling effect of live bullet screen

Table of contents 1. Basic principles 2. Specific...

The principle and implementation of js drag effect

The drag function is mainly used to allow users t...

15 JavaScript functions worth collecting

Table of contents 1. Reverse the numbers 2. Get t...

IIS7 IIS8 http automatically jumps to HTTPS (port 80 jumps to port 443)

IIS7 needs to confirm whether the "URL REWRI...

Linux sftp command usage

Concept of SFTP sftp is the abbreviation of Secur...

SQL Optimization Tutorial: IN and RANGE Queries

Preface "High Performance MySQL" mentio...

JavaScript to display hidden form text

This article shares the specific code of JavaScri...

Example of how rem is adapted for mobile devices

Preface Review and summary of mobile terminal rem...

How to mount a new disk on a Linux cloud server

background A new server was added in the company,...

How to install PHP7 Redis extension on CentOS7

Introduction In the previous article, we installe...

HTML web page image tag

Insert image tag <IMG> The colorful web page...

Scoring rules of YSlow, a webpage scoring plugin developed by Yahoo

YSlow is a page scoring plug-in developed by Yaho...