v_list_sys_cron 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/bin/bash
  2. # info: listing user cron
  3. #----------------------------------------------------------#
  4. # Variable&Function #
  5. #----------------------------------------------------------#
  6. # Argument defenition
  7. user=$1
  8. format=${2-shell}
  9. # Importing variables
  10. source $VESTA/conf/vars.conf
  11. source $V_FUNC/shared_func.sh
  12. # Json function
  13. json_list_cron() {
  14. # Print top bracket
  15. echo '{'
  16. # Count fields
  17. fileds_count=$(echo $fields| wc -w )
  18. # Reading file line by line
  19. while read line; do
  20. # New delimeter
  21. IFS=$'\n'
  22. # Assing key=value pair
  23. for key in $line; do
  24. eval ${key%%=*}=${key#*=}
  25. done
  26. # Closing bracket if there already was output
  27. if [ -n "$data" ]; then
  28. echo -e ' },'
  29. fi
  30. i=1
  31. IFS=' '
  32. for field in $fields; do
  33. eval value=\"$field\"
  34. value=$(echo "$value"|sed -e 's/"/\\"/g' -e "s/%quote%/'/g")
  35. if [ $i -eq 1 ]; then
  36. # Printing parrent
  37. (( ++i))
  38. echo -e "\t\"$value\": {"
  39. else
  40. # Printing child
  41. if [ $i -lt $fileds_count ]; then
  42. (( ++i))
  43. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  44. else
  45. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  46. data=1
  47. fi
  48. fi
  49. done
  50. done < $conf
  51. # Closing bracket if there was output
  52. if [ -n "$data" ]; then
  53. echo -e ' }'
  54. fi
  55. # Printing bottom bracket
  56. echo -e '}'
  57. }
  58. # Shell function
  59. shell_list_cron() {
  60. if [ -z "$nohead" ] ; then
  61. # Print brief info
  62. echo "${fields//$/}"
  63. for a in $fields; do
  64. echo -e "------ \c"
  65. done
  66. echo
  67. fi
  68. # Reading file line by line
  69. while read line ; do
  70. # New delimeter
  71. IFS=$'\n'
  72. # Assing key=value pair
  73. for key in $line; do
  74. eval ${key%%=*}=${key#*=}
  75. done
  76. # Print result
  77. eval echo "$fields" | sed -e "s/%quote%/'/g"
  78. done < $conf
  79. }
  80. #----------------------------------------------------------#
  81. # Verifications #
  82. #----------------------------------------------------------#
  83. # Checking args
  84. check_args '1' "$#" 'user [format]'
  85. # Checking argument format
  86. format_validation 'user'
  87. # Checking user
  88. is_user_valid
  89. #----------------------------------------------------------#
  90. # Action #
  91. #----------------------------------------------------------#
  92. # Defining config
  93. conf=$V_USERS/$user/cron.conf
  94. # Defining fileds to select
  95. fields='$JOB $MIN $HOUR $DAY $MONTH $WDAY $CMD $SUSPEND $DATE'
  96. # Listing domains
  97. case $format in
  98. json) json_list_cron ;;
  99. plain) nohead=1;
  100. fields="\"\$JOB\" \"\$SUSPEND\" \"\$MIN\" \"\$HOUR\" \"\$DAY\""
  101. fields="$fields \"\$MONTH\" \"\$WDAY\" \"\$CMD\"";
  102. shell_list_cron ;;
  103. shell) fields='$JOB~$SUSPEND~$MIN~$HOUR~$DAY~$MONTH~$WDAY~$CMD';
  104. shell_list_cron |column -t -s '~';;
  105. *) check_args '1' '0' 'user [format]' ;;
  106. esac
  107. #----------------------------------------------------------#
  108. # Vesta #
  109. #----------------------------------------------------------#
  110. # Logging
  111. log_event 'system' "$V_EVENT"
  112. exit