Skip to Content
bash shell脚本编程经典实例 (第2版)
book

bash shell脚本编程经典实例 (第2版)

by Carl Albing, JP Vossen
January 2021
Intermediate to advanced
581 pages
15h 7m
Chinese
Posts & Telecom Press
Content preview from bash shell脚本编程经典实例 (第2版)
72
4
set -e
cd mytmp
rm *
设置了
-e
标记之后,
shell
会在命令失败时退出。如果本例中的
cd
命令失败,脚本则直接
退出,不再执行
rm *
命令。但不推荐在交互式
shell
中这么做,因为如果
shell
退出,那么
终端窗口也会随之消失。
4.6.4
 参考
4.8
节讲解了
||
语法,其形式与
&&
类似,但意义大不相同。
4.7
 无人值守下运行耗时作业
4.7.1
 问题
你在后台运行了一项作业,然后退出
shell
,出去喝了杯咖啡。回来检查时发现作业已经停
止运行,而且还未完成。实际上,该作业压根就没有完成多少。你退出
shell
时,它似乎也
跟着退出了。
4.7.2
 解决方案
如果想在后台运行作业并在该作业完成前退出
shell
,那就需要对作业使用
nohup
$
nohup long &
nohup: appending output to `nohup.out'
$
4.7.3
 讨论
将作业置入后台时(通过
4.3
节中介绍过的
&
),它仍旧是
bash shell
的子进程。如果退出
shell
的某个实例,
bash
就会向其所有子进程发送
hangup
信号。这就是作业运行不了多久
的原因。只要退出
bash
,后台作业就会被“杀死”。(嗨,你要离开了。它是怎么知道的
呢?)
nohup
命令只是设置子进程忽略
hangup
信号。你仍可以用
kill
命令“杀死”作业,因为
kill
发送的是
SIGTERM
信号,而非
SIGHUP
信号。但有了
nohup
,作业就不会在退出
bash
被无意间“杀死”。
nohup
给出的那句关于追加输出的消息只是为了提高自身的实用性 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

bash 网络安全运维

bash 网络安全运维

Paul Troncone, Carl Albing
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9787115553782