怎样在后台运行 Linux 命令?(linux后台运行命令)
nanshan 2024-11-01 12:39 12 浏览 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/Unix 系统中非常常用的命令
-
Linux/Unix系统中非常常用的命令,它们是进行文件操作、文本处理、权限管理等任务的基础。下面是对这些命令的简要说明:**文件操作类:*****`ls`(list):**列出目录内容,显...
- 教你如何在Linux中删除分区(CLI篇)
-
文接上篇,继续以Ubuntu系统为例。删除分区前,急得重要数据备份!备份!备份用命令操作分区,用的最多的莫过于fdisk了,几乎所有的Linux发行版都默认带有fdisk。首先要知道的是,你想删除的分...
- 敲完就让你提桶跑路的Linux命令(敲完就让你提桶跑路的linux命令是什么)
-
不谨慎可能就会让你提桶的Linux命令!!!删除文件rm-rf该命令是删除文件或文件夹等最快的方式之一。删除后的内容很难恢复,如果删除系统文件可能会导致系统崩坏。>rm-rf/#强制...
- Log文件可以删除吗(taxukeylog文件可以删除吗)
-
Log文件(日志文件)是否可以删除取决于具体场景和文件类型。以下是详细分析和建议:一、哪些Log文件可以删除?非关键应用日志用户级应用日志:如浏览器缓存日志、游戏临时日志等,通常不影响系统运行,可定期...
- Linux 删除空目录(linux直接删除目录)
-
rmdir命令用来删除空目录。当目录不再被使用时,或者磁盘空间已到达使用限定值,就需要删除失去使用价值的目录。利用rmdir命令可以从一个目录中删除一个或多个空的子目录。该命令从一个目录中删除一个或...
- 在 Windows 11 或 10 上删除、创建和格式化分区
-
在Windows11或10上删除、创建和格式化分区假设您的现有电脑使用的是传统硬盘,但现在您想再添加一个硬盘或SSD。当然,后者将用于启动操作系统,而前者将作为纯数据存储。在成功将操作系统...
- 如何使用 Apt Clean 命令清除 APT 缓存?
-
APT(AdvancedPackageTool)是Debian系Linux发行版的包管理工具,用于处理软件包的安装、升级和依赖管理。在使用apt命令(如aptinstall、apt...
- Linux 磁盘空间不够用?5 招快速清理文件,释放 10GB 空间不是梦!
-
刚收到服务器警告:磁盘空间不足90%!装软件提示Nospaceleftondevice!连日志都写不进去,系统卡到崩溃?别慌!今天教你5个超实用的磁盘清理大招,从临时文件到无用软件一键搞定...
- Linux清空日志方法(linux怎么清理日志)
-
方法1:使用>重定向>/path/to/logfile或(需要权限时):sudosh-c'>/var/log/logfile'方法2:使用trun...
- 如何在Eclipse中搭建Zabbix源码的调试和开发环境
-
Zabbix是一款非常优秀的企业级软件,被设计用于对数万台服务器、虚拟机和网络设备的数百万个监控项进行实时监控。Zabbix是开放源码和免费的,这就意味着当出现bug时,我们可以很方便地通过调试源码来...
- Linux操作系统之常用命令(linux操作系统之常用命令有哪些)
-
Linux操作系统一、常用命令1.系统(1)系统信息arch显示机器的处理器架构uname-m显示机器的处理器架构uname-r显示正在使用的内核版本dmidecode-q显示硬件系...
- 理解linux内核的vmlinuz和initrd(linux内核原理及分析)
-
Originaladdress:http://www.chenjunlu.com/2010/11/understanding-of-vmlinuz-initrd-and-system-map/1....
- Linux纯干货知识总结|面试专用(linux面试宝典)
-
学习Linux的重要性相信不用我多说大家也明白,以下是小编总结的常用Linux基础知识以及面试常问的Linux命令,希望能帮助大家更规范地理解和使用~绝对路径和相对路径绝对路径以正斜杠开始完整的文件的...
- Linux基础知识之启动流程分析(简述linux启动流程)
-
Linux系统启动原理:1.poweron开机。2.开机自检:电脑开机后首先加载BIOS(BasicInput/OutputSystem基本输入输出系统)。BIOS程序首先检查计算机能否满足运...
- Java程序员必备——Linux的面试常见问题及面试题!你知道多少?
-
一.常用命令1.编辑相关①.awkNF:字段总数NR:第几行数据FS:分隔字符②.sed-n-i直接修改4a:在第四行后添加4i:在第四行前插入1,5csting:用sting替换1到5行...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- linux 查询端口号 (58)
- docker映射容器目录到宿主机 (66)
- 杀端口 (60)
- yum更换阿里源 (62)
- internet explorer 增强的安全配置已启用 (65)
- linux自动挂载 (56)
- 禁用selinux (55)
- sysv-rc-conf (69)
- ubuntu防火墙状态查看 (64)
- windows server 2022激活密钥 (56)
- 无法与服务器建立安全连接是什么意思 (74)
- 443/80端口被占用怎么解决 (56)
- ping无法访问目标主机怎么解决 (58)
- fdatasync (59)
- 405 not allowed (56)
- 免备案虚拟主机zxhost (55)
- linux根据pid查看进程 (60)
- dhcp工具 (62)
- mysql 1045 (57)
- 宝塔远程工具 (56)
- ssh服务器拒绝了密码 请再试一次 (56)
- ubuntu卸载docker (56)
- linux查看nginx状态 (63)
- tomcat 乱码 (76)
- 2008r2激活序列号 (65)