1. 启动命令

使用 nohup 命令挂起服务程序,启动命令如下

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

命令含义说明:

  • “nohup” :保证程序不被挂起
  • 末尾的“&”:表示后台运行程序
  • “python”:是执行python代码的命令(Python3使用python3)
  • “-u”:是为了禁止缓存,让结果可以直接进入日志文件 test_out.out(如果不加-u,则会导致日志文件不会实时刷新代码中的print函数的信息)
  • “test.py”:要执行的python的源代码文件
  • “>”:是指将打印信息指定到日志文件
  • “test_out.out”:是输出的日志文件
  • “2>&1”:将标准错误输出转变化标准输出,可以将错误信息也输出到日志文件中(0-> stdin, 1->stdout, 2->stderr)

2. 检查是否成功

使用 jobs 命令:

[root@localhost test]# jobs
[4]+  Running                 nohup python -u test.py > test_out.out 2>&1 &

使用 ps -ef | grep python 或者 ps -ef | grep Job 命令,查看进程

# ps -ef|grep python# ps -ef|grep Job

使用 ps aux 命令,查看程序的进程号

[root@localhost test]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     22246  0.0  0.3 125572  6304 pts/0    S    17:29   0:00 python -u test.py

3. 关闭挂起进程

使用 kill -9 进程号,关闭指定进程号的程序

[root@localhost test]# kill -9 60000
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐