由于在shell脚本中直接使用&符号和python程序有冲突,不能正常的进入后台执行python(多番尝试python xxx.py &,后面的&总是不能正常识别.系统centos7).所以需要用到几种不同的方式来启动python
- nohup
nohup python run_server.py
- ()
(python run_server.py)
- screen 推荐的方式
screen python run_server.py
脚本中使用
#!/bin/bash
# echo "/bin/bash /opt/startjms.sh" and chmod +x /etc/rc.d/rc.local
# env
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
PATH=$PATH:$HOME/bin
export PATH
source /opt/py3/bin/activate
#open screen terminal
screen_name1="server1"
screen -dmS $screen_name1
screen_name2="server2"
screen -dmS $screen_name2
#trans cmd to screen terminal
screen -x -S $screen_name1 -p 0 -X stuff "python /opt/$screen_name1/run_server.py all\n"
cmd="python /opt/$screen_name2/run_server.py"
screen -x -S $screen_name2 -p 0 -X stuff "$cmd"
screen -x -S $screen_name2 -p 0 -X stuff $'\n'
- systemd
这些都不能的话,老老实实用systemctl吧