问题
链接服务器后,想在服务器上跑一个耗时特别长的程序
问题是一旦会话结束,程序也就结束了
解决
使用 nohup 命令挂起服务
例如我要执行我的 mytest.py 脚本程序
[root@localhost test]# nohup python3 -u mytest.py > mytest_out.out 2>&1 &
[4] 22246
[root@localhost test]#
-u 是为了禁止缓存,让结果可以直接进入日志文件 mytest_out.out
在这里,mytest_out.out 是我的 mytest.py 输出的内容
接下来,我们要看看它是否是在后台执行
使用命令 jobs
[root@localhost test]# jobs
[4]+ Running nohup python3 -u mytest.py > mytest_out.out 2>&1 &
[root@localhost test]#
如果我们想要结束这个后台执行的程序要怎么办呢?
那就是查看进程,前面的 22246 就是这个程序的进程号,当然了,我们也是可以查看的,使用命令 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 python3 -u