当前位置:网站首页>Additional Features for Scripting
Additional Features for Scripting
2022-08-01 22:59:00 【梦想家DBA】
1.1 "Daemon-izing" Your Script
Use the following to invoke your script, run it in the background, and still allow yourself to log out:
[[email protected] test]$ nohup mydaemonscript 0<&- 1>/dev/null 2>&1 &
[1] 209178
[[email protected] test]$ nohup mydaemonscript >>/var/log/myadmin.log 2>&1 <&- &
[2] 209179
[1] Exit 127 nohup mydaemonscript 0>&- > /dev/null 2>&1
[[email protected] test]$ -bash: /var/log/myadmin.log: Permission denied
[2]+ Exit 1 nohup mydaemonscript >> /var/log/myadmin.log 2>&1 0>&-
[[email protected] test]$ But what about STDIN? The cleanest way to deal with STDIN is to close the file descriptor. The bash syntax to do that is like a redirect, but with a dash for the filename (0<&- or <&-).
We use the nohup command so that the script is run without being interrupted by a hangup signal when we log off.
1.2 Reusing Code with Includes and Sourcing
[[email protected] test]$ sh -x use_prefs.sh
+ source /home/maxwell/shelllearning/test/myprefs.cfg
++ SCRATCH_DIR=/var/tmp
++ IMG_FMT=png
++ SND_FMT=ogg
+ cd /var/tmp
+ echo You prefer png image files
You prefer png image files
+ echo You prefer ogg sound files
You prefer ogg sound files
[[email protected] test]$ cat myprefs.cfg
SCRATCH_DIR=/var/tmp
IMG_FMT=png
SND_FMT=ogg
[[email protected] test]$ cat use_prefs.sh
#!/bin/bash
#
# use the user prefs
#
source $HOME/shelllearning/test/myprefs.cfg
cd ${SCRATCH_DIR:-/tmp}
echo You prefer $IMG_FMT image files
echo You prefer $SND_FMT sound files
[[email protected] test]$ 1.5 Using Functions: Parameters and Return Values
# define the function:
function max ( )
{ ... }
#
# call the function:
#
max 128 $SIM
max $VAR $CNT[[email protected] test]$ sh -x max 128 $SIM
[[email protected] test]$ echo $BIGR
[[email protected] test]$ cat max
#!/bin/bash
#cookbook filename: func_max
# define the function:
function max ()
{
local HIDN
if [ $1 -gt $2 ]
then
BIGR=$1
else
BIGR=$2
fi
HIDN=5
}
[[email protected] test]$ 1.6 Trapping Interrupts
Use the trap utility to set signal handlers. First, use trap -l (or kill -l) to list the signals you may trap. They vary from system to system:
[[email protected] test]$ trap -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
[[email protected] test]$ [[email protected] test]$ cat hard_to_kill.sh
#!/bin/bash -
trap ' echo "You got me! $?" ' ABRT EXIT HUP INT TERM QUIT
trap ' echo "Later... $?"; exit ' USR1
sleep 120
[[email protected] test]$ sh -x hard_to_kill.sh
+ trap ' echo "You got me! $?" ' ABRT EXIT HUP INT TERM QUIT
+ trap ' echo "Later... $?"; exit ' USR1
+ sleep 120
^C++ echo 'You got me! 130'
You got me! 130
+ echo 'You got me! 130'
You got me! 130
[[email protected] test]$ sh -x hard_to_kill.sh &
[1] 209366
[[email protected] test]$ + trap ' echo "You got me! $?" ' ABRT EXIT HUP INT TERM QUIT
+ trap ' echo "Later... $?"; exit ' USR1
+ sleep 120
[[email protected] test]$ kill -USR1 %1
[[email protected] test]$ User defined signal 1
++ echo 'Later... 138'
Later... 138
++ exit
+ echo 'You got me! 0'
You got me! 0
[1]+ Done sh -x hard_to_kill.sh
[[email protected] test]$ sh -x hard_to_kill.sh &
[1] 209374
[[email protected] test]$ + trap ' echo "You got me! $?" ' ABRT EXIT HUP INT TERM QUIT
+ trap ' echo "Later... $?"; exit ' USR1
+ sleep 120
[[email protected] test]$ kill %1
[[email protected] test]$ Terminated
++ echo 'You got me! 143'
You got me! 143
+ echo 'You got me! 143'
You got me! 143
[1]+ Exit 143 sh -x hard_to_kill.sh
[[email protected] test]$ 边栏推荐
- 数据增强--学习笔记(图像类,cnn)
- blender3.2.1 unit setting
- 小程序毕设作品之微信体育馆预约小程序毕业设计成品(2)小程序功能
- From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program
- PHP算法之有效的括号
- 小程序中的多表联合查询
- 复现gallerycms字符长度限制短域名绕过
- 域名重定向工具 —— SwitchHosts 实用教程
- JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
- 分享10套开源免费的高品质源码,免费源码下载平台
猜你喜欢

域名重定向工具 —— SwitchHosts 实用教程

罗克韦尔AB PLC RSLogix5000中的比较指令使用方法介绍

How to add a game character to a UE4 scene

img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)

CAKE:一个用于多视图知识图谱补全的可扩展性常识感知框架

xctf attack and defense world web master advanced area webshell

Mini Program Graduation Works WeChat Food Recipe Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template

小程序容器+自定义插件,可实现混合App快速开发

一种灵活的智能合约协作方式

APP专项测试:流量测试
随机推荐
Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
杭电多校3 1012. Two Permutations dp*
威纶通触摸屏如何打开并升级EB8000旧版本项目并更换触摸屏型号?
一种灵活的智能合约协作方式
最短路模板
PDF转Word有那么难吗?做一个文件转换器,都解决了
浅析多服务在分布式系统下多事务通信处理机制方案
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D 题解
毕业作业
线上故障排查方案
华为无线设备配置双链路冷备份(AP指定配置方式)
perspectiveTransform warpPerspective getPerspectiveTransform findHomography
03、GO语言变量定义、函数
JS 数组去重(含简单数组去重、对象数组去重)
Small application project works WeChat stadium booking applet graduation design of the finished product (1) the development profile
leetcode刷题
npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
自建 Prometheus 采集腾讯云容器服务监控数据最佳实践
SRv6 L3VPN的工作原理
The must-have "fishing artifact" for programmers is here!