【问题标题】:How to use init-functions? [closed]如何使用初始化函数? [关闭]
【发布时间】:2017-09-11 20:39:16
【问题描述】:

每个操作系统都有库。它们中的大多数让你有机会做更多的事情来编写更少的代码。 inti-functions 是这些库之一,但是...

什么是初始化函数以及如何使用它?

【问题讨论】:

  • init-functions 是 SysV init 的产物,一般来说,它是一段过时的代码,永远不应该用于新的开发。现代最佳实践是使用适当的进程监督系统(systemd、upstart、runit、launchd 等)——不是传统的“初始化脚本”。
  • smarden.org/runit/runscripts.html 上查看示例运行脚本——您会注意到它们几乎普遍低于五行total,因此不需要任何类型图书馆的支持。其他过程监控系统的配置可能不那么简洁,但它们使 init 函数过时且无关紧要。

标签: linux bash shell unix


【解决方案1】:

在 Linux 中,您可以使用初始化函数以彩色和统一的方式管理守护进程和协议。这比重新着色每个答案更容易处理。

它存储在/lib/lsb/ 中,名称为init-functions

这里有一个例子:

#!/bin/bash
# Script by Amir Boudani                      ©Amir Boudani
source /lib/lsb/init-functions                  # Importing init-functions - log messages
                                # Use for importing other scripts ore else: "source" / "."
                                # Use at the beginning of the path "/" to say that the path has to start from the root directory

log_success_msg "Starting test.sh" || true
echo
log_action_msg "Info message" || true               # Info message
log_success_msg "Checking Files" || true            # Success message
log_warning_msg "Free storage is under 10%" || true         # Warning message
log_failure_msg "Starting unknown script files" || false    # Failure message

echo
log_end_msg 0 || true                       # End message - success - resetting the line above -> log_daemon_msg
log_end_msg 1 || true                       # End message - failure - resetting the line above -> log_daemon_msg
log_end_msg 255 || true                     # End message - warning - resetting the line above -> log_daemon_msg
echo
log_progress_msg "copying file 2123"                # One of the line resetting comands - progress
echo
echo

log_daemon_msg "Integrate files" || true
sleep 2
log_end_msg 0 || true

log_daemon_msg "Checkt if the directory 4234 exist" || true
sleep 2
log_end_msg 1 || true

log_daemon_msg "Storage space checking " || true
sleep 2
log_end_msg 255 || true
echo
echo
log_daemon_msg "Checking Filestorage" || true
sleep 1
log_progress_msg "Checking File 1"  || true
sleep 1
log_progress_msg "Checking File 2" || true
sleep 1
log_progress_msg "Checking File 3"  || true
sleep 1
log_progress_msg "Checking File 4"  || true
sleep 1
log_end_msg 0 || true

echo

#---------------------------------------------------------------# Method 1 - not working for commands that are self responding

$checker                            # A variable that doesn't exist. To trigger an error code
log_daemon_msg "Check if the last command was an Error" || true
if [ "$?" -eq "0" ]                         # "$?" is the errorcode number of the last command. | 0 = There was no Errors | 1 = Error code 1
then
    log_end_msg 0 || true
else
    log_end_msg 1 || true
    echo "       There went something wrong. see log"
fi

#---------------------------------------------------------------# A way to check if a directory exist

#if [ -d "/opt/scripts"  ]                  # Check if directory exist - example at this line: if directory /opt/scripts exist
                                # -d = check if the following directory exitst
                                # -a = check if the following file exsist
#if [ ! -d "/opt/scripts" ]                 # ! is a negativ operator - example at this line: if directory /opt/scripts NOT exist
                                # You can actually use the ! operator anywhere

#---------------------------------------------------------------# Method 2 - working for self responding commands. but the log_daemon_msg ist not working so we take log_succes/warning/failure_msg

echo
echo "type in: false / true / (nothing)"
read var1 
case $var1 in                           # Check if variable var1 is true
    true)
        log_success_msg "Check if var1 is true" || true
        ;;

    false)
                log_warning_msg "Check if var1 is true" || true
                echo -e "       \e[33mvar1 is false.\e[0m"
                ;;

    *)
            log_failure_msg "Check if var1 is true" || true
            echo -e "       \e[31mthere was no input in the read statment.\e[0m"
        ;;
esac

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    相关资源
    最近更新 更多