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

跟散仙学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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics