-3
A
回答
-1
在Linux中,您可以使用init函數以統一的方式管理deamons和協議,並使用有色的&。這比處理每一個答案更容易處理。
它以名稱init-functions
存儲在/lib/lsb/
中。
這裏有一個例子:
#!/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
相關問題
- 1. 如何用函數更改ng-init值?
- 2. 如何從(init)JSON函數調用(ko.observableArray)?
- 3. jQuery init()函數。
- 4. 我如何使用init函數創建dylib
- 5. 如何讓OpenGlView的init函數運行?
- 6. 如何在init中添加函數
- 7. Kotlin Array init函數
- 8. 當我提供init()函數時,爲什麼會調用init(coder :)
- 9. 身體onload init()函數
- 10. init函數和在Javascript
- 11. PhoneGap Facebook插件Init()函數
- 12. AngularJS構造函數或init?
- 13. 如何使用angularjs在單個ng-init中直接調用多個函數
- 14. 如何用茉莉花單元測試init()函數?
- 15. 如何在AngularJS中調用ng-init之前執行JS函數?
- 16. 如何在Init方法中調用函數?
- 17. Swift3,如何調用ClassA類函數ClassB的的init()
- 18. JavaScript:調用init函數與返回該函數調用
- 19. 如何使點擊工作以及如何在init中延遲函數? XCODE
- 20. 使用javascript在facebook init上調用函數時出錯?
- 21. 重複調用嵌入在init函數中的函數
- 22. 如何使用「git --bare init」存儲庫?
- 23. 如何在init鉤子中使用get_permalink()
- 24. 何時使用alloc init
- 25. 如何在Swift中的兩個init函數之間共享初始化函數?
- 26. 讓這個未使用的函數有什麼意義Init
- 27. 爲什麼要使用類方法構造函數與alloc/init?
- 28. 爲什麼在Objective C構造函數中使用[super init]?
- 29. 使用easeljs時未定義的init()函數
- 30. 爲什麼我需要返回「init」來執行init函數?
'INIT-functions'是SysV初始化,這是一般來說代碼的陳舊塊,它不應該被用於新發展的神器。現代最佳實踐是使用適當的過程監督系統(systemd,upstart,runit,launchd等) - *不是傳統的「init腳本」。 –
請參閱http://smarden.org/runit/runscripts.html上的示例運行腳本 - 您會注意到它們幾乎全部在五行*總計*之下,因此不需要任何類型的庫支持。其他過程監督系統的配置可能不那麼簡單,但它們使init函數過時並且不相關是普遍的。 –