`
qindongliang1922
  • 浏览: 2171452 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
7265517b-f87e-3137-b62c-5c6e30e26109
证道Lucene4
浏览量:117083
097be4a0-491e-39c0-89ff-3456fadf8262
证道Hadoop
浏览量:125428
41c37529-f6d8-32e4-8563-3b42b2712a50
证道shell编程
浏览量:59501
43832365-bc15-3f5d-b3cd-c9161722a70c
ELK修真
浏览量:71020
社区版块
存档分类
最新评论

跟散仙学shell编程(六)

阅读更多
上篇介绍了linux里面的处理数据的方式,本篇散仙来说下,如何在linux里面控制脚本的执行。


在linux里面我们最常遇见的几个linux信号如下:
序号信号描述
11sighup挂起进程
22sigint终止进程
33sigquit停止进程
49sigkill无条件终止进程
515sigterm可能的话终止进程
617sigstop无条件停止进程,但不是终止进程
718sigtstp停止或暂停进程,但不终止进程
819sigcont继续运行停止的进程



在shell脚本里,Ctrl+C命令会产生sigint终止进程的信号,所以会强制退出任何在运行的进程:
[search@h1 814]$ sleep 30
^C
[search@h1 814]$ 

Ctrl+Z的组合键,会产生一个Sigtstp的信号,停止shell中运行的任何进程,如果你在此时退出shell进程,shell会提醒你,你有停止的进程:
[search@h1 814]$ sleep 30
^Z
[1]+  Stopped                 sleep 30
[search@h1 814]$ exit
logout
There are stopped jobs.
[search@h1 814]$ ps au
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1112  0.0  0.0   4064   580 tty1     Ss+  02:59   0:00 /sbin/mingetty /dev/tty1
root      1114  0.0  0.0   4064   576 tty2     Ss+  02:59   0:00 /sbin/mingetty /dev/tty2
root      1116  0.0  0.0   4064   576 tty3     Ss+  02:59   0:00 /sbin/mingetty /dev/tty3
root      1118  0.0  0.0   4064   576 tty4     Ss+  02:59   0:00 /sbin/mingetty /dev/tty4
root      1120  0.0  0.0   4064   580 tty5     Ss+  02:59   0:00 /sbin/mingetty /dev/tty5
root      1122  0.0  0.0   4064   580 tty6     Ss+  02:59   0:00 /sbin/mingetty /dev/tty6
root      1132  0.0  0.0 108432  1968 pts/0    Ss   03:00   0:00 -bash
root      1359  0.0  0.0 145432  1584 pts/0    S    04:19   0:00 su - search
search    1360  0.0  0.0 108432  2024 pts/0    S    04:19   0:00 -bash
search    1383  0.0  0.0 100904   596 pts/0    T    04:21   0:00 sleep 30
search    1384  1.0  0.0 110228  1172 pts/0    R+   04:22   0:00 ps au
[search@h1 814]$ 


找到pid,我们就可以使用我们经常用的kill -9 pid来,为T的进程


在linux里面捕捉信号,我们可以使用trap命令:
[search@h1 814]$ cat a.sh 


trap "echo '我捕捉到了Ctrl+C的信号'"  SIGINT SIGTERM






sleep 20





echo `date`
[search@h1 814]$ sh a.sh 
^C我捕捉到了Ctrl+C的信号
2014年 08月 15日 星期五 04:35:25 CST
[search@h1 814]$ 


trap命令会捕捉此信号,并阻止其停止进程。
当然我们还可以捕捉,脚本的退出:
[search@h1 814]$ cat aa.sh 
#!/bin/bash
trap "退出了"  EXIT




sleep 5





echo "最后执行!!!!"

[search@h1 814]$ sh aa.sh 
最后执行!!!!
aa.sh: line 1: 退出了: command not found


我们可以使用trap - EXIT命令移除捕捉

[search@h1 814]$ cat b.sh 



trap "退出了:"  EXIT


sleep 5



echo `date`


trap - EXIT

echo "移除捕捉"
[search@h1 814]$ sh b.sh 
2014年 08月 15日 星期五 04:56:20 CST
移除捕捉
[search@h1 814]$ sh b.sh 
^Cb.sh: line 1: 退出了:: command not found

[search@h1 814]$ 



下面我们来看下,如何在后台模式运行,脚本:

[search@h1 814]$ cat bb.sh 




for ((  i=0 ; i< 10 ;i++  ))
do
 
 echo "序号: $i"

 sleep 1


done

[search@h1 814]$ sh bb.sh 
序号: 0
序号: 1
序号: 2
序号: 3
序号: 4
序号: 5
序号: 6
序号: 7
序号: 8
序号: 9
[search@h1 814]$ 

如果我们想后台模式执行,可以在脚本后面加个&符号:
[search@h1 814]$ sh bb.sh &
[2] 1455
[search@h1 814]$ 序号: 0
序号: 1
序号: 2
序号: 3
序号: 4
序号: 5
序号: 6
序号: 7
序号: 8
序号: 9

[2]-  Done         


方括号里面的是作业号,后面的是进程,我们可以通过ps au命令查看。


注意&和nohup的区别,就在于&在shell终端关闭时,任务就会终止,如果你不想这么样,你可以使用nohup命令来实现,同样式刚才的例子:
[search@h1 814]$ nohup sh bb.sh & 
nohup: 忽略输入并把输出追加到"nohup.out"




[search@h1 814]$ 
[search@h1 814]$ 
[search@h1 814]$ 
[search@h1 814]$ 
[search@h1 814]$ ll
总用量 32
-rw-rw-r-- 1 search search  77 8月  15 04:47 aa.sh
-rw-rw-r-- 1 search search  95 8月  15 04:35 a.sh
-rw-rw-r-- 1 search search  75 8月  15 05:01 bb.sh
-rw-rw-r-- 1 search search  87 8月  15 04:56 b.sh
-rw-rw-r-- 1 search search  20 8月  15 05:05 c1.sh
-rw-rw-r-- 1 search search  20 8月  15 05:05 c2.sh
-rw-rw-r-- 1 search search  20 8月  15 05:05 c.sh
-rw------- 1 search search 100 8月  15 05:13 nohup.out
[search@h1 814]$ cat nohup.out 
序号: 0
序号: 1
序号: 2
序号: 3
序号: 4
序号: 5
序号: 6
序号: 7
序号: 8
序号: 9
[search@h1 814]$ 


nohup命令,会把后台运行中的输出充重定向到nohup.out里面,如果有错误则输出到nohup.err里面。

查看作业使用jobs命令可以查看,后台运行情况:
[search@h1 814]$ nohup sh bb.sh &
[2] 1550
[search@h1 814]$ nohup: 忽略输入并把输出追加到"nohup.out"

[search@h1 814]$ job
-bash: job: command not found
[search@h1 814]$ jobs
[1]+  Stopped                 sleep 30
[2]-  Running                 nohup sh bb.sh &
[search@h1 814]$ 


[search@h1 814]$ cat bb.sh 





echo "进程ID:$$"

for ((  i=0 ; i< 10 ;i++  ))
do
 
 echo "序号: $i"

 sleep 1


done

[search@h1 814]$ nohup sh bb.sh &
[2] 1564
[search@h1 814]$ nohup: 忽略输入并把输出追加到"nohup.out"

[search@h1 814]$ jobs
[1]+  Stopped                 sleep 30
[2]-  Running                 nohup sh bb.sh &
[search@h1 814]$ 


$$可以打印当前进程id
要以后台模式重启一个作业,可以使用bg命令加上作业号,
如果以前台模式重启一个作业,可以使用fg命令,加上作业号:

除以之外,我们还可以使用nice命令来调整任务的优先级别,范围从-20到20 ,数值越大优先级越低,
nice -n 10 nohup sh bb.sh &
[2] 1576
[1]   Exit 125                nice -n 10nohup sh bb.sh
[search@h1 814]$ nohup: 忽略输入并把输出追加到"nohup.out"

[search@h1 814]$ ps al
F   UID   PID  PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4     0  1112     1  20   0   4064   580 n_tty_ Ss+  tty1       0:00 /sbin/mingetty /dev/tty1
4     0  1114     1  20   0   4064   576 n_tty_ Ss+  tty2       0:00 /sbin/mingetty /dev/tty2
4     0  1116     1  20   0   4064   576 n_tty_ Ss+  tty3       0:00 /sbin/mingetty /dev/tty3
4     0  1118     1  20   0   4064   576 n_tty_ Ss+  tty4       0:00 /sbin/mingetty /dev/tty4
4     0  1120     1  20   0   4064   580 n_tty_ Ss+  tty5       0:00 /sbin/mingetty /dev/tty5
4     0  1122     1  20   0   4064   580 n_tty_ Ss+  tty6       0:00 /sbin/mingetty /dev/tty6
4     0  1132  1127  20   0 108432  1968 wait   Ss   pts/0      0:00 -bash
4     0  1359  1132  20   0 145432  1584 wait   S    pts/0      0:00 su - search
4   500  1360  1359  20   0 108432  2056 wait   S    pts/0      0:00 -bash
4     0  1503  1498  20   0 108432  1968 wait   Ss   pts/1      0:00 -bash
4     0  1518  1503  20   0 145432  1584 wait   S    pts/1      0:00 su - search
4   500  1519  1518  20   0 108428  1984 n_tty_ S+   pts/1      0:00 -bash
0   500  1576  1360  30  10 106060  1348 wait   SN   pts/0      0:00 sh bb.sh
0   500  1581  1576  30  10 100904   596 hrtime SN   pts/0      0:00 sleep 1
0   500  1582  1360  20   0 108124  1040 -      R+   pts/0      0:00 ps al
[search@h1 814]$ 


renice命令,可以改变已有进程的优先级别,使用root用户可以调整任意优先级,其他用户则有限制。



在linux中执行定时的任务主要有at和crontab两种方法:
如果系统没有都可执行yum install at 或yum install crond下载安装
命令格式at [-f filename ] time
查询作业atq,删除作业使用atrm 作业号


at命令适合用在预设时间内执行脚本比较好,如果需要每天或每周甚至每月跑一次,我们就可以使用cron程序,cron在实际开发中,用的比较多,例如散仙公司用到的定时建索引的服务就是以crontab设定的,关于crond的使用,请参考散仙以前的文章


小技巧:如何在每月的最后一天执行某个任务用法

00 12 * * * if [ `date +%d -d tomorrow` = 01 ] ; then ; command

在linux里面还有一个anacron程序,类似cron,它可以在错过的计划内,尽快运行某次因服务挂掉,或机器停电的作业,有兴趣的朋友可以自己查阅下。



如果我们想把我们的脚本,开始时运行起来,就需要把我们的脚本放在/etc/init.d/rc.local里面或/etc/rc.local里面,不同的linux的发行版本可能不太一样,通常,我们看一下就能识别出来。

[search@h1 814]$ cd /etc/init.d/
[search@h1 init.d]$ ll
总用量 168
-rwxr-xr-x  1 root root  2062 1月  30 2012 atd
-rwxr-xr-x. 1 root root  3378 6月  22 2012 auditd
-r-xr-xr-x. 1 root root  1340 11月 24 2013 blk-availability
-rwxr-xr-x. 1 root root  2826 11月 23 2013 crond
-rw-r--r--. 1 root root 18586 10月 10 2013 functions
-rwxr-xr-x. 1 root root  5866 10月 10 2013 halt
-rwxr-xr-x. 1 root root 10804 11月 23 2013 ip6tables
-rwxr-xr-x. 1 root root 10688 11月 23 2013 iptables
-rwxr-xr-x. 1 root root  4535 10月  8 2013 iscsi
-rwxr-xr-x. 1 root root  3990 10月  8 2013 iscsid
-rwxr-xr-x. 1 root root   652 10月 10 2013 killall
-r-xr-xr-x. 1 root root  2134 11月 24 2013 lvm2-lvmetad
-r-xr-xr-x. 1 root root  2665 11月 24 2013 lvm2-monitor
-rwxr-xr-x. 1 root root  2571 10月 11 2013 mdmonitor
-rwxr-xr-x. 1 root root  2523 11月 23 2013 multipathd
-rwxr-xr-x  1 root root  7026 2月  13 2014 mysqld
-rwxr-xr-x. 1 root root  2989 10月 10 2013 netconsole
-rwxr-xr-x. 1 root root  5428 10月 10 2013 netfs
-rwxr-xr-x. 1 root root  6334 10月 10 2013 network
-rwxr-xr-x. 1 root root  3852 12月  3 2011 postfix
-rwxr-xr-x. 1 root root  1513 9月  17 2013 rdisc
-rwxr-xr-x. 1 root root  1822 11月 23 2013 restorecond
-rwxr-xr-x. 1 root root  2011 8月  15 2013 rsyslog
-rwxr-xr-x. 1 root root  1698 11月 23 2013 sandbox
-rwxr-xr-x. 1 root root  2056 11月 20 2012 saslauthd
-rwxr-xr-x. 1 root root   647 10月 10 2013 single
-rwxr-xr-x. 1 root root  4534 11月 23 2013 sshd
-rwxr-xr-x. 1 root root  2294 11月 23 2013 udev-post
[search@h1 init.d]$ 


如果想在登陆shell时,执行一些脚本,通常我们在用户根目录下的.bash_profile和.bashrc文件里面来测试,注意这两个文件为隐藏文件,需要使用ls -al命令查看
下面散仙测试:

-rw-------.  1 search search      9909 8月  15 05:28 .bash_history
-rw-r--r--.  1 search search        18 7月  18 2013 .bash_logout
-rw-r--r--.  1 search search       176 7月  18 2013 .bash_profile
-rw-r--r--.  1 search search       124 7月  18 2013 .bashrc



在.bashrc里面加一条输出,每次退出(Ctrl+D)登陆(Enter),都会打印:
[root@h1 ~]# su - search
欢迎使用!!
[search@h1 ~]$ cat .bashrc 
# .bashrc

# Source global definitions



if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

echo "欢迎使用!!"

# User specific aliases and functions
[search@h1 ~]$ 



0
0
分享到:
评论

相关推荐

    跟老男孩学Linux运维:Shell编程实战 PDF

    跟老男孩学Linux运维:Shell编程实战 PDF跟老男孩学Linux运维:Shell编程实战 PDF

    跟老男孩学Linux运维:Shell编程实战

    《跟老男孩学Linux运维:Shell编程实战》分为五大部分:部分为第1章~第4章,此部分着重介绍新手如何学好Shell编程。涉及的内容包括Shell编程的入门介绍、基础知识、运行原理、编程语法、编程习惯、变量知识以及变量...

    跟老男孩学Linux运维:Shell编程实战.pdf 高清 带书签

    资深运维架构实战专家及教育培训界*专家十多年的运维实战经验总结,全面系统地讲解运维工作中Shell编程所需的知识点和Shell编程的各种企业级案例。

    shell编程入门经典--LINUX与UNIX Shell编程指南 (中文pdf版)

    《LINUX与UNIX Shell编程指南》是一本专为初学者设计的shell编程教程,它深入浅出地介绍了在Linux和UNIX系统中如何使用Shell进行高效自动化任务处理。Shell编程是Linux和UNIX系统中的核心技术,它允许用户通过命令行...

    shell编程指南shell编程指南shell编程指南shell编程指南

    Shell编程是一种在Unix/Linux操作系统环境下进行系统管理与自动化任务的强大工具。它允许用户通过命令行接口执行各种操作,包括文件管理、程序控制、系统管理任务等。本指南将深入探讨Shell编程的基础知识、常用命令...

    Windows Shell 编程.pdf

    Windows Shell 编程.pdf 看过一些对windows 外壳的扩展程序,在使用上一般都是直接利用windows的外壳API做一些工作,因为外壳操作需要一些比较专业的知识,因此,大部分编程人员特别是使用集成编程环境的程序人员对...

    shell编程学习资料

    Shell编程是Linux/Unix系统中不可或缺的一部分,它是一种命令行解释器,允许用户与操作系统进行交互,执行系统命令,以及编写脚本自动化任务。在本文中,我们将深入探讨Shell编程的基础知识,包括基本命令、变量、...

    shell编程入门教程+shell脚本专家指南+UNIX.shell编程24小时教程.rar

    《shell编程入门教程》、《shell脚本专家指南》以及《UNIX.shell编程24小时教程》会提供详尽的实例和练习,帮助你巩固所学并深化理解。 总之,Shell编程是Linux/Unix环境中不可或缺的技能,它能够提高工作效率,...

    实验六_shell编程.docx

    下面是实验六 Shell 编程实验的详细知识点: 一、实验准备 在开始实验之前,需要登录系统,使用实验一创建的用户名和密码,并打开 Terminal。 二、观察比较:UNIX shell 脚本与 Linux shell 脚本的格式区别 在这...

    shell编程题目练习

    shell编程题目练习,练习基本的shell编程,学习脚本语言,提高效率

    跟老男孩学Linux运维:Shell编程实战 完整版 pdf

    跟老男孩学Linux运维:Shell编程实战 完整版 pdf

    Linux与UNIX Shell编程指南.pdf

    "Linux与UNIX Shell编程指南" Linux与UNIX Shell编程指南是计算机科学领域中一本经典的指南手册,旨在帮助读者快速掌握Linux与UNIX操作系统下的shell编程技术。下面是从该书中生成的相关知识点: 1. Shell概述 ...

    Windows Shell 编程指南与实例

    《Windows Shell 编程指南与实例》是一本深入探讨Windows操作系统壳层编程技术的专业书籍。在Windows系统中,Shell指的是用户界面,它为用户提供与操作系统交互的环境,包括桌面、开始菜单、快捷方式等。Shell编程则...

    Shell编程高级进阶系列视频.zip

    13Linux下Shell编程之While case演练 14Linux下Shell编程之While case演练 15Shell编程之函数及脚本案例讲解 16Shell编程之函数及脚本案例讲解 17Linux下Shell编程FIND、SED命令实战 18Linux下Shell编程FIND、SED...

    UNIX命令及SHELL编程

    这是一套完整的Unix培训教材,包括Unix常用命令及SHELL编程基础与高级技巧,PDF格式,共30个文件。另有2个Word文档。包内文件清单如下: 01_Shell-文件安全与权限.PDF 02_Shell-使用find和xargs.PDF 03_Shell-...

    Shell编程中文手册.pdf

    Shell 编程中文手册 本手册涵盖了 Shell 编程的基础知识,包括 Shell 概述、Shell 解析器、Shell 脚本入门、Shell 中的变量等。 Shell 概述 Shell 是一种命令行接口,允许用户与操作系统进行交互。学习 Shell ...

    Unix Shell Shell编程

    6本pdf及chm的shell 编程的书 6本pdf及chm的shell 编程的书 6本pdf及chm的shell 编程的书 6本pdf及chm的shell 编程的书 6本pdf及chm的shell 编程的书

    shell编程个人笔记

    shell编程个人笔记shell编程个人笔记shell编程个人笔记shell编程个人笔记shell编程个人笔记shell编程个人笔记shell编程个人笔记shell编程个人笔记shell编程个人笔记shell编程个人笔记shell编程个人笔记shell编程个人...

    shell demo及编程pdf

    **Shell编程介绍** Shell编程是Linux/Unix操作系统中的一种脚本语言,用于自动化日常任务,交互式地控制操作系统,以及实现系统级别的程序间交互。它提供了命令行接口(CLI)来执行各种系统命令,使用户能够高效地...

Global site tag (gtag.js) - Google Analytics