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

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

nanshan 2024-11-01 12:39 13 浏览 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
$
$

相关推荐

轻量级分析利器再升级:解读 DuckDB 1.3.0 新特性

DuckDB团队近日正式发布了最新版本——DuckDB1.3.0,代号“Ossivalis”。此次版本以金眼鸭的远古祖先BucephalaOssivalis命名,象征项目在演化和成长过...

C++跨平台编译的终极奥义:用Docker把环境差异按在地上摩擦

"代码在本地跑得飞起,一上服务器就coredump?"——每个C++程序员都经历过的《编译器的复仇》事件!大家好,我是Henry,废话少说,今天来简单谈一下跨平台编译的那些事儿,...

全网最全-Version Script以及__asm__((&quot;.symver xxx&quot;))使用总结

首先提醒一点,一切的前提建立在你的名字必须要mangling,不然无论你写的versionscript还是__asm__都不会起任何效果VersionScript简单用法:这是一个典型例子,这个例...

Ubuntu 25.04 Beta发布:Linux 6.14内核

IT之家3月28日消息,Canonical昨日(3月27日)放出了Beta版Ubuntu25.04系统镜像,代号“PluckyPuffin”,稳定版预估将于2025年...

不同平台CRT的区别?什么是UCRT?如何看libc源代码?

若文章对您有帮助,欢迎关注程序员小迷。助您在编程路上越走越好!CRT运行时库C标准规定例如输入输出函数、字符串函数、内存操作等接口,一般采用C运行时库实现。微软的CRT微软有两套CRT,早期的MS...

信创力量,中兴绽放——中兴新支点桌面操作系统安装与使用全攻略

原文链接:「链接」Hello,大家好啊,今天给大家带来一篇中兴新支点桌面操作系统安装使用的文章,欢迎大家分享点赞,点个在看和关注吧!中兴新支点桌面操作系统是一款基于Linux内核、面向政企和信创环...

Linux下安装常用软件都有哪些?做了一个汇总列表,你看还缺啥?

1.安装列表MySQL5.7.11Java1.8ApacheMaven3.6+tomcat8.5gitRedisNginxpythondocker2.安装mysql1.拷贝mysql安装文件到...

一篇文章解决Linux系统安全问题排查,另配实操环境

实操地址:https://www.skillup.host/1/linux/safe/command.md#Linux安全检查排查指南##1.系统账户安全检查###1.1检查异常账户``...

程序员必备的学习笔记《TCP/IP详解(一)》

为什么会有TCP/IP协议在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别。就好像圣经中上帝打乱了各地人的口音,让他们无法合作一样...

《Linux常用命令》(linux的常用命令总结)

一、文件与目录操作1.目录导航pwd:显示当前工作目录路径示例:pwd关键词:当前路径、工作目录cd:切换目录示例:cd/home/user#切换到绝对路径cd..#...

Kubernetes 教程之跟着官方文档从零搭建 K8S

前言本文将带领读者一起,参照者Kubernetes官方文档,对其安装部署进行讲解.Kubernetes更新迭代很快,书上、网上等教程可能并不能适用于新版本,但官方文档能.阅读这篇文章你...

电脑网卡坏了怎么修复(电脑网卡坏了怎么修复win7系统)

当电脑网卡出现故障时,无论是有线网络还是无线网络,都可能无法正常连接。下面从软件、硬件等方面,分步骤为你介绍排查与修复的解决方案。一、初步排查:锁定问题源头检查网络环境将手机、平板等其他设备连接至同一...

如何查询电脑/手机的物理地址(如何找手机的物理地址)

一、要查询电脑的物理地址(也称为MAC地址),可以按照以下步骤进行操作:1.打开命令提示符(Windows)或终端(Mac):-在Windows上,点击“开始”按钮,搜索“命令提示符”,然后点击打...

IPv4 无网络访问权限全流程解决方案

当设备出现IPv4无网络访问权限问题时,多由网络配置错误、连接故障或服务异常导致。以下提供系统化的排查步骤与解决方案,帮助用户快速定位并修复问题。一、基础故障快速检查1.物理连接确认有线网络:检...

Python教程(十九):文件操作(python操作文件夹)

昨天,我们学习了列表推导式,掌握了Python中最优雅的数据处理方式。今天,我们将学习文件操作—Python中读写文件的基础技能。文件操作是编程中的核心技能,无论是读取配置文件、保存用户数据,还是...

取消回复欢迎 发表评论: