• Additional Features for Scripting


    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:

    1. [maxwell@MaxwellDBA test]$ nohup mydaemonscript 0<&- 1>/dev/null 2>&1 &
    2. [1] 209178
    3. [maxwell@MaxwellDBA test]$ nohup mydaemonscript >>/var/log/myadmin.log 2>&1 <&- &
    4. [2] 209179
    5. [1] Exit 127 nohup mydaemonscript 0>&- > /dev/null 2>&1
    6. [maxwell@MaxwellDBA test]$ -bash: /var/log/myadmin.log: Permission denied
    7. [2]+ Exit 1 nohup mydaemonscript >> /var/log/myadmin.log 2>&1 0>&-
    8. [maxwell@MaxwellDBA 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

    1. [maxwell@MaxwellDBA test]$ sh -x use_prefs.sh
    2. + source /home/maxwell/shelllearning/test/myprefs.cfg
    3. ++ SCRATCH_DIR=/var/tmp
    4. ++ IMG_FMT=png
    5. ++ SND_FMT=ogg
    6. + cd /var/tmp
    7. + echo You prefer png image files
    8. You prefer png image files
    9. + echo You prefer ogg sound files
    10. You prefer ogg sound files
    11. [maxwell@MaxwellDBA test]$ cat myprefs.cfg
    12. SCRATCH_DIR=/var/tmp
    13. IMG_FMT=png
    14. SND_FMT=ogg
    15. [maxwell@MaxwellDBA test]$ cat use_prefs.sh
    16. #!/bin/bash
    17. #
    18. # use the user prefs
    19. #
    20. source $HOME/shelllearning/test/myprefs.cfg
    21. cd ${SCRATCH_DIR:-/tmp}
    22. echo You prefer $IMG_FMT image files
    23. echo You prefer $SND_FMT sound files
    24. [maxwell@MaxwellDBA test]$

    1.5 Using Functions: Parameters and Return Values

    1. # define the function:
    2. function max ( )
    3. { ... }
    4. #
    5. # call the function:
    6. #
    7. max 128 $SIM
    8. max $VAR $CNT
    1. [maxwell@MaxwellDBA test]$ sh -x max 128 $SIM
    2. [maxwell@MaxwellDBA test]$ echo $BIGR
    3. [maxwell@MaxwellDBA test]$ cat max
    4. #!/bin/bash
    5. #cookbook filename: func_max
    6. # define the function:
    7. function max ()
    8. {
    9. local HIDN
    10. if [ $1 -gt $2 ]
    11. then
    12. BIGR=$1
    13. else
    14. BIGR=$2
    15. fi
    16. HIDN=5
    17. }
    18. [maxwell@MaxwellDBA 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:

    1. [maxwell@MaxwellDBA test]$ trap -l
    2. 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
    3. 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
    4. 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
    5. 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
    6. 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
    7. 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
    8. 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
    9. 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
    10. 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
    11. 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
    12. 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
    13. 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
    14. 63) SIGRTMAX-1 64) SIGRTMAX
    15. [maxwell@MaxwellDBA test]$

    1. [maxwell@MaxwellDBA test]$ cat hard_to_kill.sh
    2. #!/bin/bash -
    3. trap ' echo "You got me! $?" ' ABRT EXIT HUP INT TERM QUIT
    4. trap ' echo "Later... $?"; exit ' USR1
    5. sleep 120
    6. [maxwell@MaxwellDBA test]$ sh -x hard_to_kill.sh
    7. + trap ' echo "You got me! $?" ' ABRT EXIT HUP INT TERM QUIT
    8. + trap ' echo "Later... $?"; exit ' USR1
    9. + sleep 120
    10. ^C++ echo 'You got me! 130'
    11. You got me! 130
    12. + echo 'You got me! 130'
    13. You got me! 130
    14. [maxwell@MaxwellDBA test]$ sh -x hard_to_kill.sh &
    15. [1] 209366
    16. [maxwell@MaxwellDBA test]$ + trap ' echo "You got me! $?" ' ABRT EXIT HUP INT TERM QUIT
    17. + trap ' echo "Later... $?"; exit ' USR1
    18. + sleep 120
    19. [maxwell@MaxwellDBA test]$ kill -USR1 %1
    20. [maxwell@MaxwellDBA test]$ User defined signal 1
    21. ++ echo 'Later... 138'
    22. Later... 138
    23. ++ exit
    24. + echo 'You got me! 0'
    25. You got me! 0
    26. [1]+ Done sh -x hard_to_kill.sh
    27. [maxwell@MaxwellDBA test]$ sh -x hard_to_kill.sh &
    28. [1] 209374
    29. [maxwell@MaxwellDBA test]$ + trap ' echo "You got me! $?" ' ABRT EXIT HUP INT TERM QUIT
    30. + trap ' echo "Later... $?"; exit ' USR1
    31. + sleep 120
    32. [maxwell@MaxwellDBA test]$ kill %1
    33. [maxwell@MaxwellDBA test]$ Terminated
    34. ++ echo 'You got me! 143'
    35. You got me! 143
    36. + echo 'You got me! 143'
    37. You got me! 143
    38. [1]+ Exit 143 sh -x hard_to_kill.sh
    39. [maxwell@MaxwellDBA test]$
  • 相关阅读:
    Git 的安装和配置
    NVIDIA Jetson测试安装yolox过程记录
    SpringBoot篇---第二篇
    Linux中的DNS服务搭建与管理
    操作系统(四)文件管理
    Welcome to YARP - 5.身份验证和授权
    C#手动填充DataSet
    从统计语言模型到预训练语言模型---预训练语言模型(Transformer)
    电子管晶体管示波器电路图
    前端工程化精讲第十五课 版本特性:Webpack 5 中的优化细节
  • 原文地址:https://blog.csdn.net/u011868279/article/details/126086044