> 文章列表 > ubuntu常用命令

ubuntu常用命令

ubuntu常用命令

查询进程:

ps -ef

杀死进程

kill -9

 killall -9 chrome

killall chromedriver  杀死所有chrome

ps  ef 查看运行的任务

ps -ef | grep python 

top 查看运行任务详情

后台启动Python脚本

可以使用下面的命令来启动上面的脚本,让Python在后台运行。

nohup python -u main.py > test.out 2>&1 &

其中 0、1、2分别代表如下含义:
 0 – stdin (standard input)
 1 – stdout (standard output)
 2 – stderr (standard error)
nohup python -u main.py > test.out 2>&1 &
nohup+最后面的& 是让命令在后台执行
>out.log 是将信息输出到out.log日志中
2>&1 是将标准错误信息转变成标准输出,这样就可以将错误信息输出到out.log 日志里面来。

跟踪输出文件变化

为了验证脚本可以在后台继续运行,我们退出当前会话。然后重新连接一个Session,然后输入下面的命令来跟踪文件的输出:

tail -f test.out
 

结束程序

可以直接通过之前的那个pid杀掉脚本,或者可以通过下面的命令查找pid。

 ps -ef | grep python

kill -9 9208
 

 

 

编写启动及停止脚本

启动脚本

#!/bin/sh

pid=`ps -ef|grep "python -u main.py"| grep -v "grep"|awk '{print $2}'`

if [ "$pid" != "" ]
then
        echo "main.py already run, stop it first"
        kill -9 ${pid}
fi

echo "starting now..."

nohup python -u main.py > test.out 2>&1 &

pid=`ps -ef|grep "python -u main.py"| grep -v "grep"|awk '{print $2}'`

echo ${pid} > pid.out
echo "main.py started at pid: "${pid}
 

停止脚本 

 

#!/bin/sh

pid=`ps -ef|grep "python -u main.py"| grep -v "grep"|awk '{print $2}'`

if [ "$pid" != "" ]
then
        kill -9 ${pid}
        echo "stop main.py complete"
else
        echo "main.py is not run, there's no need to stop it"
fi