当前位置:网站首页>Learning a subshell and process of the Shell system
Learning a subshell and process of the Shell system
2022-08-07 16:16:00 【edenliuJ】
系列文章目录
ShellWhat is system learningShell
ShellThe system learns to create oneShell程序
ShellHow the system learns to performShell程序
Shell系统学习之Shell条件测试,Judgment Statements and Operators
ShellA function of system learning
ShellAn array of systematic learning
ShellFile operations for system learning
ShellThe son of systematic learningShelldeal with the process
文章目录
目录
Shell内部命令、External commands and reserved words
通过信号和trapCommand control process
前言
当我们在执行Shell脚本时,会感觉到ShellThe script is currentShellInterpreted at the command line prompt,其实不是的,,when the script is executed,当前的ShellWill start a child process to execute the current oneShell脚本.This article will explain itShell和进程处理
子Shell
什么是子Shell
Let's get to know the first one firstShell是如何启动的?
当用户登录Linux后,The operating system will vary according to the user/etc/passwdThe configuration in the file starts oneShell进程,该ShellProcesses are executed by the current userShell命令的父进程.Each user can specify an automatic defaultShell程序,The sections are shown below/etc/passwd的内容:
[email protected]:~/Documents/Shell/12$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
其中/etc/passwdEach line describes a user information,一共有7个字段,每个字段的含义如下:
用户名:x:用户ID:用户组ID:备注信息:用户主目录:默认Shell程序
默认ShellPrograms use absolute paths.if this fieldShell程序不存在,不合法,You cannot log in to the host.If the user does not need to log in,then you can set the path to /usr/sbin/nologin.
The diagram below depicts father and sonShell的工作过程:

Shell内部命令、External commands and reserved words
在一些其他书籍中,We often see the so-called internal orders and external orders of this term,Here's an explanation of their differences.
Internal commands are meant to be contained withinShell工具包中的命令,Internal commands are embedded in Shell程序中,Not as a separate disk file(可执行文件)存在.比如cd, bg, exit,exec, eval,pwd等.
外部命令是Linux系统中的实用程序部分,These programs exist on disk as disk files.在用户登录时,External commands do not come with defaults like internal commandsShell加载到内存中,Instead, it is called into memory when needed.ShellThe program manages path lookups and code loading when external commands are executed,并控制命令的执行.比如ls,at,du,host等都是外部命令.External commands are usually located in /usr/bin以及/usr/sbin等目录中,其中/usr/sbinThe commands in are generally related to system administration.
ShellThe reserved words are generally Shell语法中的关键字,这里不一一列出了.
在子Shell中执行命令
(command1;command2;...)
(command1;command2;...)&
When the command is executed within a parenthesis,The set of commands will be in the subShell中执行 ,If added after parentheses&,表示该子Shell后台执行,Does not block the current parent process.
Know this method first,There are places to look at later,I don't feel like it's working yet,但是要了解,Know when you see this kind of code.
进程处理
What are processes and jobs
什么是进程,这里就不多做介绍了,Let's first understand the concept of homework:A job can contain multiple processes,Jobs are for users,A process started by a user to complete a task,1jobs can contain1个,也可以包含多个进程,These processes work together,共同完成任务.The process is for the operating system,It is the basic unit of operating system program execution.
比如:
ls -l | grep 13 | more The above set of commands can be called a job,But it will still start3个进程.
when we execute in the backgroundShell脚本时,比如:
[email protected]:~/Documents/Shell/13$ ./13-10.sh &
[1] 16386
会显示一个[1] 和 16386
其中[1]中的1表示作业号,16386Indicates the number of the process executing the script.
You will be prompted when the job is complete:
[1]+ Done ./13-10.sh
The above prompt includes the job number 状态 The script command executed
When executing the foreground job again,可以执行Ctrl+Z将当前任务挂起,比如我执行vi 命令编辑一个文件,然后按Ctrl+Z的效果:
[email protected]:~/Documents/Shell/13$ vi 13-2.sh
[2]+ Stopped vi 13-2.sh
The job number indicated above is 2's job has been suspended.
相关命令fd,jobs,disown
使用fd命令,是一个内部命令,Move the job from the background to the foreground to run
fd [jobspec]
jobsepcUsed to specify the job to switch,fdSpecifies the method for switching jobs:
| 方法 | 说明 |
|---|---|
| %n | n为整数,表示作业号 |
| %string | 以stringThe job corresponding to the command starting with the string |
| %?string | 包含stringThe job corresponding to the string command |
| %+或者%% | Recently submitted assignments |
| %- | The second-to-last submitted assignment |
jobs命令View a list of jobs that are executing in the background,基本语法如下:
jobs [options]
[email protected]:~/Documents/Shell/13$ jobs
[2]- Stopped vi 13-2.sh
[3]+ Stopped vi 13-2.sh
其中[3]旁边的+Indicates the most recent job,[2]旁边的-The sign indicates the second-to-last job.
作业的状态:
| 状态 | 说明 |
|---|---|
| Running | 正在运行 |
| Stopped | 被挂起 |
| Done | The job has completed with exit code 0 |
| Done(code) | The job completed normally and exited,退出状态码非0,状态码使用10进制表示 |
disown命令Used to delete a job in the job list
disown [jobsepc]
用法与fd命令一样.
通过信号和trapCommand control process
kill Commands can pass signals to processes.
kill -lThe command displays the signals supported by the current system:
[email protected]:~/Documents/Shell/13$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
也可以通过man 7 signalCheck out the help manual for signals.
| 信号 | 值 | 含义 |
|---|---|---|
| SIGHUP | 1 | 终端挂起或者控制进程终止 |
| SIGINT | 2 | 键盘中断 |
| SIGQUIT | 3 | The keyboard escape key is pressed |
| SIGABRT | 6 | 由about(3)发出的退出指令 |
| SIGKILL | 9 | 立即结束进程 |
Use belowkillCommand syntax for signaling a process:
kill [-s signal] pidfor some signals,The process will take the default response action,And some signal processes may be ignored directly.在Shell中用户可以通过trap命令to set an action in response to a signal.trap是一个内部命令,语法如下:
trap [[arg] signspec ...]
举例:
#!/bin/bash
function signal_handler()
{
echo "Good bye"
}
trap signal_handler 0在POSIXStandard center handle0Defined as a null signal,在程序退出时会触发,该程序的执行结果:
[email protected]:~/Documents/Shell/13$ ./13-11.sh
Good bye
小结
This article focuses on father and sonShell,内部命令和外部命令,前后台运行,Processes and jobs and control over them.
边栏推荐
- jmter regex extractor
- 2.10 - 存储管理 2.11 - 页式存储 2.12 - 段式存储 2.13 - 段页式存储
- 关于将本地 SAP UI5 应用配置到本地 Fiori Launchpad 的技术实现深入讲解试读版
- 网上注册证券账户安全吗 网上开户需要多少钱
- LeetCode Daily 2 Questions 01: Rotating Arrays (both 1200 questions)
- 美团一面面经及详细答案
- vite+ts项目中如何设置alias
- LeetCode每日两题01:旋转数组(均1200道)
- mysql5.7.35安装配置教程【超级详细安装教程】
- 5 years of experience, never heard of XFF vulnerabilities
猜你喜欢
随机推荐
在树莓派上使用cpolar(番外篇1)
873. 欧拉函数
mysql5.7安装和配置教程(图文超详细版)
熟练使用几个重要的内置函数
「短视频」甘肃省出台“十四五”冷链物流高质量发展方案 打造1小时鲜活农产品物流圈
from . import XXX
网上注册证券账户安全吗 网上开户需要多少钱
pytorch保存训练日志
MySQL5.7 installation and configuration tutorial (graphics and text super detailed version)
【21天学习挑战赛】双向链表的数据操作
MySQL5.7.35 installation and configuration tutorial [super detailed installation tutorial]
grouped knapsack problem
LeetCode每日两题01:删除排序数组中的重复项 (均1200道)
现在网上开户安全?想知道股票开账户如何优惠开户?
When Oracle11G uses EXP to export, the empty table cannot be exported.
如何得到股票开户的优惠活动?网上开户是否安全?
研扬Jetson NX镜像备份和恢复
jmter regex extractor
通过dlv简单分析Go coredump文件
SAP 电商云 Spartacus UI 的 External Routes 设计明细









