百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

怎样在后台运行 Linux 命令?(linux后台运行命令)

nanshan 2024-11-01 12:39 7 浏览 0 评论

一般我们使用 shell 终端时,都是与终端交互式的一行行敲命令进行执行。但是当某个命令执行过长时,导致我们无法与终端进行交互执行下一条命令。


比如,如下脚本



$ cat test.py
#!/usr/bin/python

#-*- encoding: utf-8 -*-
import time

for i in range(1,5000):
time.sleep( 5 )
print "hello"
else:
print "The end"

由于该脚本长时间运行,导致后续的命令无法交互执行


$ python test.py
hello
hello
...

那么怎么解决呢?


  • 重新打开一个新的 shell 进行执行命令。
  • 让进程在后台运行。


那么让进程在后台运行的方式有哪些呢 ?


& 让命令在后台运行


在命令后面加 &,可以让命令在后台执行。


$ python test.py &
[1] 2640
hello
hello
$ ls
test.py 

当我们让进程在后天运行时,我们可以继续与终端交互。


但是可以看到一个问题,那就是打印的结果还是会在终端显示,这明显对工作进行干扰,我们可以采用将执行后的命令输出结果重定向到文件中。


> 重定向输出


可以通过 > 把脚本输出结果重定向到具体文件中。


$ python test.py > out.txt &
[2] 2961

$ ls
out.txt test.py 
[2]+ Done python test.py > out.txt

$ cat out.txt
hello
hello
...
The end

脚本 test.py 在后台运行时,我们可以继续执行其他命令,当脚本运行完后,我们可以通过 out.txt 查看脚本输出结果。


但是当脚本执行时出现异常,异常信息会丢失而不会保存到 out.txt 文件中:


$ cat test.py
#!/usr/bin/python

#-*- encoding: utf-8 -*-
import time

for i in range(1,5):
# time.sleep( 5 )
print "hello"
if i == 2 :
i = i / 0
else:
print "The end"

$ python test.py > out.txt &
[2] 4257
$ Traceback (most recent call last):
File "test.py", line 10, in <module>
i = i / 0
ZeroDivisionError: integer division or modulo by zero
^C
[2]+ Exit 1 python test.py > out.txt
$
$ cat out.txt
hello
hello
$


2>&1 将标准出错重定向到标准输出


为了解决异常信息保存到文件中,我们可以采用把标准错误和标准输出都重定向到文件中:


$ python test.py > out.txt 2>&1 &
[2] 4377
$ cat out.txt
hello
hello
Traceback (most recent call last):
File "test.py", line 10, in <module>
i = i / 0
ZeroDivisionError: integer division or modulo by zero
[2]+ Exit 1 python test.py > out.txt 2>&1

$


上面的调用表明将 test.py 的输出重定向到 out.txt 文件中,同时将标准错误也重定向到 out.txt 文件中。


比如我们运行test.py 脚本时通过查看 /proc/进程id/fd 下的内容,可了解进程打开的文件描述符信息


$ python test.py > out.txt 2>&1 &
[1] 4467
$
$ cd /proc/4467/fd
$ ll
total 0
lrwx------. 1 jim jim 64 Feb 7 20:31 0 -> /dev/pts/0
l-wx------. 1 jim jim 64 Feb 7 20:31 1 -> /home/study/out.txt
l-wx------. 1 jim jim 64 Feb 7 20:31 2 -> /home/study/out.txt
$

从中我们可以看到,脚本运行过程中,标准输出和标准错误都重定向到了out.txt 中。


但是上述还有一个问题,当我们把终端关闭时,我们的后台程序也就终止了:


在终端一中 执行脚本
$ python test.py > out.txt 2>&1 &
[2] 4548


在终端二中 查看后台进程
$ ps -ef | grep test
jim 4548 2374 0 20:39 pts/0 00:00:00 python test.py
jim 4642 4591 0 20:39 pts/1 00:00:00 grep --color=auto test

关闭终端一,在终端二中查看后台进程
$ ps -ef | grep test
jim 4659 4591 0 20:39 pts/1 00:00:00 grep --color=auto test
$


nohup 退出终端后,程序依然后台执行


nohup:no hang up,不挂起的意思,其用途就是让提交的命令忽略 hangup 信号。


在终端一中 执行脚本
$ nohup python test.py > out.txt 2>&1 &
[2] 4548


关闭终端一,在终端二中查看后台进程
$ ps -ef | grep test
jim 4548 2374 0 20:39 pts/0 00:00:00 python test.py
jim 4642 4591 0 20:39 pts/1 00:00:00 grep --color=auto test

从中可以看到,终端虽然关闭了,脚本依然在继续执行。


那么当后台运行的进程过多时,怎么查看后台进程呢?


jobs 查看后台执行的进程


我们可以通过 jobs 命令查看运行的后台进程,最后启动的放在最后边。


$ jobs
[1]- Running python test.py &
[2]+ Running python test.py > out.txt 2>&1 &

怎么把后台执行的命令重新调到前端执行呢?


fg 把后台进程返回到前台执行


$ python test.py > out.txt 2>&1 &
[1] 5049
$ python test.py > out.txt 2>&1 &
[2] 5050
$ python test.py > out.txt 2>&1 &
[3] 5051
$ jobs
[1] Running python test.py > out.txt 2>&1 &
[2]- Running python test.py > out.txt 2>&1 &
[3]+ Running python test.py > out.txt 2>&1 &
$
$ fg
python test.py > out.txt 2>&1

^C
$ jobs
[1]- Running python test.py > out.txt 2>&1 &
[2]+ Running python test.py > out.txt 2>&1 &
$ 

可以看到 fg 把第三个后台进程放到前台执行了。


fg + 序号 可以指定某个后台进程在前台执行:


$ jobs
[1] Running python test.py > out.txt 2>&1 &
[2]- Running python test.py > out.txt 2>&1 &
[3]+ Running python test.py > out.txt 2>&1 &
$
$ fg 2
python test.py > out.txt 2>&1
^C
$
$ jobs
[1]- Running python test.py > out.txt 2>&1 &
[3]+ Running python test.py > out.txt 2>&1 &
$


怎样暂停某个进程呢?


Ctrl+z 暂停某个进程



$
$ jobs
$ nohup python test.py > out.txt 2>&1
^Z // ctrl + z
[1]+ Stopped nohup python test.py > out.txt 2>&1
$
$ jobs
[1]+ Stopped nohup python test.py > out.txt 2>&1
$

可以看到进程状态为 暂停状态。


怎么继续执行被暂停的后台进程呢?


bg 继续执行后台暂停的进程


$
$ bg
[1]+ nohup python test.py > out.txt 2>&1 &
$
$ jobs
[1]+ Running nohup python test.py > out.txt 2>&1 &
$


怎么终止某个进程呢?


kill 终止进程


可以通过 jobs -l 命令或者ps命令获取 pid 号,然后进行 kill。



$ jobs -l
[3]- 5373 Running nohup python test.py > out.txt 2>&1 &
[4]+ 5374 Running nohup python test.py > out.txt 2>&1 &
$
$ ps -ef | grep test
jim 5373 4838 0 21:16 pts/1 00:00:00 python test.py
jim 5374 4838 0 21:16 pts/1 00:00:00 python test.py
jim 5376 4838 0 21:16 pts/1 00:00:00 grep --color=auto test
$
$ kill 5374
$
[4]+ Terminated nohup python test.py > out.txt 2>&1
$ jobs -l
[3]+ 5373 Running nohup python test.py > out.txt 2>&1 &
$
$ ps -ef | grep test
jim 5373 4838 0 21:16 pts/1 00:00:00 python test.py
jim 5379 4838 0 21:17 pts/1 00:00:00 grep --color=auto test
$
$

相关推荐

超详细!Linux文件和目录的10项属性

公众号:老油条IT记文件和目录10项属性目录1:索引节点:inode2:文件类型3:链接数4:用户5:组6:文件大小7.8.9:时间戳10:文件名1.Linux文件#概述#1.linux里一切皆为文件...

如何恢复 Linux 系统下被删除的文件 ?

丢失数据是任何用户都可能经历的最令人不安和痛苦的经历之一。一旦珍贵数据被删除或丢失,就再也找不不回来通常会引发焦虑,让用户感到无助。值得庆幸的是,有几个工具可以用来恢复Linux机器上被删除的文件...

Linux文件系统操作常用命令

在Linux系统中,有一些常用的文件系统操作命令,以下是这些命令的介绍和作用:#切换目录,其中./代表当前目录,../代表上一级目录cd#查看当前目录里的文件和文件夹ls#...

Linux系统下使用lsof工具恢复rm命令删除的文件

rm(Remove)和lsof(ListOpenFiles)是Linux命令行工具,直接操作文件系统。rm用于删除文件和目录;lsof用于查看进程打开的文件、网络连接、设备等信息。rm删除的文...

Linux文件管理知识:文本处理

Linux文件管理知识:文本处理上篇文章详细介绍了Linux系统中查找文件的工具或者命令程序的相关操作内容介绍。那么,今天呢,这篇文章围绕Linux系统中文本处理来阐述。众所周知,所有Linux操作系...

Linux基础运维篇:Linux磁盘与文件系统管理(第012课)

一、磁盘基础认知1.磁盘是什么在Linux系统里,磁盘就像是一个巨大的仓库,专门用来存放各种数据。电脑里的文档、图片、程序等,都储存在磁盘上。磁盘有不同的类型,常见的有机械硬盘(HDD)和固态硬...

Linux系统中其他值得关注的病毒/恶意软件示例

Linux系统中其他值得关注的病毒/恶意软件示例,结合其传播方式、危害特征及清除方法进行整理。一、经典病毒家族1.Slapper特征:利用Apache的SSL漏洞传播的蠕虫病毒,可创建僵尸网络供攻击者...

Linux磁盘爆满紧急救援指南:5步清理释放50GB+小白也能轻松搞定

“服务器卡死?网站崩溃?当Linux系统弹出‘Nospaceleft’的红色警报,别慌!本文手把手教你从‘删库到跑路’进阶为‘磁盘清理大师’,5个关键步骤+30条救命命令,快速释放磁盘空间,拯救你...

Linux常用文件操作命令

ls命令在Linux维护工作中,经常使用ls这个命令,这是最基本的命令,来写几条常用的ls命令。先来查看一下使用的ls版本#ls--versionls(GNUcoreutils)8.4...

linux怎么编辑文件内容

在Linux中,你可以使用多种方法来编辑文件内容。以下是几种常用的方法:使用文本编辑器:你可以使用命令行下的文本编辑器,如vi、vim或nano来编辑文件。例如,使用vim编辑一个名为example....

linux学习笔记——常用命令-文件处理命令

ls目录处理命令:ls全名:list命令路径:/bin/ls执行权限:所有用户ls–ala--alll–long-i查看i节点ls–i查看i节点命令名称:mkdir命令英文原意:m...

Win10新版19603推送:一键清理磁盘空间、首次集成Linux文件管理器

继上周四的Build19592后,微软今晨面向快速通道的Insider会员推送Windows10新预览版,操作系统版本号Build19603。除了一些常规修复,本次更新还带了不少新功能,一起来了...

很少有人知道可以这样删除文件

有时候我们在格式化硬盘分区或者删除一些文件的时候,会出现无法操作的情况,例如下面这种这个文件权限问题,系统为了保护一些文件而采取的安全措施,如果你能确定文件是可能删除的,那你只要赋予它管理员权限,...

linux中磁盘满了?一招教你快速清理

创作背景:当天部署服务时,发现无法部署,后来经过日志排查后发现服务器磁盘满了,查询资料后进行了清理。话不多说,直接上解决方法。操作一:1.查看磁盘大小:df-h2.直接在最上层进行排序:du-a...

Linux下乱码的文件名修改或删除

查看文件名#lstouch1?.txt#ll-itotal1469445217956913-rw-r--r--1oracleoinstall0Jan18...

取消回复欢迎 发表评论: