Execute multiple py files under the shell terminal

1 Execute a single py file

Modify it python_pathto your own python installation path
Modified cdpath

#!/bin/bash
python_path="/usr/local/bin/python3"
# "statistic.py"
files=("test.py")
start() {
    
    
  cd /public/home/data/
  
  for i in ${
    
    files[*]}; do
    pid=$(ps aux | grep python | grep "${i}" |grep -v grep| awk '{print $2}')
    if [ ! "$pid" ]; then
      ${
    
    python_path} "${i}" &
    else
    echo "${i} Started!"
    fi
  done
 
}
stop() {
    
    
  pids=()
  pid=$(ps aux | grep uvicorn|grep -v grep | awk '{print $2}')
  pids[${
    
    #pids[@]}]=$pid
  pid=$(ps aux | grep multiprocessing |grep -v grep | awk '{print $2}')
  pids[${
    
    #pids[@]}]=$pid
  pid=$(ps aux | grep main|grep -v grep | awk '{print $2}')
  pids[${
    
    #pids[@]}]=$pid


  for i in ${
    
    files[*]}; do
    pid=$(ps aux | grep python | grep "${i}"|grep -v grep | awk '{print $2}')
    if [ ! "$pid" ]; then
      echo "${i} Stopped!"
    else
      pids[${
    
    #pids[@]}]=$pid
    fi
  done
  for p in ${
    
    pids[*]}; do
    kill -9 "${p}"
    echo "${p} closed"
  done
}
restart() {
    
    
  stop
  start
}
#main function
case $1 in
start)
  start
  ;;
stop)
  stop
  ;;
restart)
  restart
  ;;
esac

2 Execute multiple py files

2.1 Sequential execution

modified cdpath

#!/bin/bash

cd /public/home/data/test/
python3 test1.py
python3 test2.py
python3 test3.py
python3 test4.py
python3 test5.py
python3 test6.py

! If it is uploaded to the liunx server from windows, the shell script executes an error: /bin/bash^M: bad interpreter: No such file or directory
Reason : the file in the windows environment is in dos format, and the end of each line is marked with \r\n; The file under linux is in unix format, and the end of the line is marked with \n.

Check the file : 1. In the output of
cat -A test.sh, the end of the line is ^M, which is in dos format, and if the end of the line is just, it is in unix format. 2.vim test.sh, execute ":set ff", if the execution result is fileformat=dos, it will be in dos format, if the execution result is fileformat=unix, it will be in unix format

Solution:
1. sed -i "s/\r//" test.sh or sed -i "s/^M//" test.sh, directly replace the carriage return with an empty string.
2. vim test.sh, execute ": set ff=unix", set the file to unix format, save and exit.

2.2 Multi-process execution

Modify it python_pathto your own python installation path
Modified cdpath

#!/bin/bash
python_path="/usr/local/bin/python3"  
# "statistic.py"
cd /public/home/data/test/

start() {
    
    
  for dir in $(ls ./); 
  do
    pid=$(ps aux | grep python | grep "${dir}" |grep -v grep| awk '{print $2}')
	echo "${pid} Started!"
    if [ ! "$pid" ]; then
      ${
    
    python_path} "${dir}" &
    else
    echo "${dir} Started!"
    fi
  done
 
}
stop() {
    
    
  pids=()
  pid=$(ps aux | grep uvicorn|grep -v grep | awk '{print $2}')
  pids[${
    
    #pids[@]}]=$pid
  pid=$(ps aux | grep multiprocessing |grep -v grep | awk '{print $2}')
  pids[${
    
    #pids[@]}]=$pid
  pid=$(ps aux | grep main|grep -v grep | awk '{print $2}')
  pids[${
    
    #pids[@]}]=$pid


  for dir in $(ls ./); do
    pid=$(ps aux | grep python | grep "${i}"|grep -v grep | awk '{print $2}')
    if [ ! "$pid" ]; then
      echo "${dir} Stopped!"
    else
      pids[${
    
    #pids[@]}]=$pid
    fi
  done
  for p in ${
    
    pids[*]}; do
    kill -9 "${p}"
    echo "${p} closed"
  done
}
restart() {
    
    
  stop
  start
}
#main function
case $1 in
start)
  start
  ;;
stop)
  stop
  ;;
restart)
  restart
  ;;
esac

Note: If you want to print the execution process and execution results of the script to the log, execute

./run.sh restart 2>&1 |tee -a mylog.log

run.shFor the execution script, mylog.logfor the log file

Guess you like

Origin blog.csdn.net/weixin_50008473/article/details/126439260