Python runs programs automatically in the background

table of Contents

Method 1: Run the program in the background (add "&"):

Method two, write bash script

Method three, use nohup command

1. View all background processes:

or:

2. View the specified process, such as uwsgi:

3. Kill the specified process:


(Personally think method three nohup command is the most effective)

Method 1: Run the program in the background (add "&"):

python run.py &

Note: If you use ssh to connect to the server, you need to use the command: exit to exit, do not close it directly, otherwise the corresponding process will be closed.

Method two, write bash script

Create .sh file

touch start.sh

Write code:

vi start.sh

====>

/root/anaconda3/envs/myenv/bin/python scheduled_task.py &

Start script:

start.sh 或者 ./start.sh

Method three, use nohup command

nohup python scheduled_task.py &

Nohup command introduction:

       Purpose: LINUX command usage, run commands without hanging up. 
  Syntax : nohup Command [Arg ...] [&] 
  Description : The nohup command runs the command specified by the Command parameter and any related Arg parameters, ignoring all hang-up (SIGHUP) signals. Use the nohup command to run programs in the background after logging off. To run the nohup command in the background, add & (the symbol for "and") to the end of the command. 
  If you do not redirect the output of the nohup command, the output will be appended to the nohup.out file in the current directory. If the nohup.out file in the current directory is not writable, the output is redirected to the $HOME/nohup.out file. If no file can be created or opened for appending, the command specified by the Command parameter cannot be called. If standard error is a terminal, all output written to standard error of the specified command is redirected to the same file descriptor as standard output. 
  Exit status : The command returns the following exit values: 
  126 The command specified by the Command parameter can be found but cannot be called. 
  127 An error occurred in the nohup command or the command specified by the Command parameter could not be found. 
  Otherwise, the exit status of the nohup command is the exit status of the command specified by the Command parameter. 
  The nohup command and its output file 
  nohup command: If you are running a process, and you think the process will not end when you log out of your account, you can use the nohup command. This command can continue to run the corresponding process after you log out of the account/close the terminal. Nohup means not to hang up (n ohang up). 
  The general form of the command is:nohup command & 

Use the nohup command to submit a job: 
  If you use the nohup command to submit a job, all the output of the job will be redirected to a file named nohup.out by default, unless you specify an output file: 
  nohup command> myout .file 2>&1 & 
  In the above example, the output is redirected to the myout.file file.
  Use jobs to view tasks.
  Use fg %n to close

      Note: When exiting the sh connection, you still have to use the exit command.

1. View all background processes:

ps -ef

or:

ps -aux

2. View the specified process, such as uwsgi:

ps -ef|grep uwsgi

3. View the process occupying a certain port:

centos :

ss -lntpd | grep :8088

4. Kill the specified process:

kill pid(指定的进程)

 

Guess you like

Origin blog.csdn.net/weixin_38664232/article/details/107959035