v-add-cron-job 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. # info: add cron job
  3. # options: USER MIN HOUR DAY MONTH WDAY COMMAND [JOB] [RESTART]
  4. #
  5. # The function adds a job to cron daemon. When executing commands, any output
  6. # is mailed to user's email if parameter REPORTS is set to 'yes'.
  7. #----------------------------------------------------------#
  8. # Variable&Function #
  9. #----------------------------------------------------------#
  10. # Argument definition
  11. user=$1
  12. min=$2
  13. hour=$3
  14. day=$4
  15. month=$5
  16. wday=$6
  17. command=$(echo $7 |sed "s/'/%quote%/g")
  18. job=$8
  19. restart=$9
  20. # Includes
  21. source $VESTA/func/main.sh
  22. source $VESTA/conf/vesta.conf
  23. HIDE=7
  24. #----------------------------------------------------------#
  25. # Verifications #
  26. #----------------------------------------------------------#
  27. check_args '7' "$#" 'USER MIN HOUR DAY MONTH WDAY COMMAND [JOB] [RESTART]'
  28. is_format_valid 'user' 'min' 'hour' 'day' 'month' 'wday' 'command'
  29. is_system_enabled "$CRON_SYSTEM" 'CRON_SYSTEM'
  30. is_object_valid 'user' 'USER' "$user"
  31. is_object_unsuspended 'user' 'USER' "$user"
  32. is_package_full 'CRON_JOBS'
  33. get_next_cronjob
  34. is_format_valid 'job'
  35. is_object_new 'cron' 'JOB' "$job"
  36. #----------------------------------------------------------#
  37. # Action #
  38. #----------------------------------------------------------#
  39. # Generating timestamp
  40. time_n_date=$(date +'%T %F')
  41. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  42. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  43. # Concatenating cron string
  44. str="JOB='$job' MIN='$min' HOUR='$hour' DAY='$day' MONTH='$month' WDAY='$wday'"
  45. str="$str CMD='$command' SUSPENDED='no' TIME='$time' DATE='$date'"
  46. # Adding to crontab
  47. echo "$str" >> $VESTA/data/users/$user/cron.conf
  48. # Changing permissions
  49. chmod 660 $VESTA/data/users/$user/cron.conf
  50. # Sort jobs by id number
  51. sort_cron_jobs
  52. # Sync cronjobs with system crond
  53. sync_cron_jobs
  54. #----------------------------------------------------------#
  55. # Vesta #
  56. #----------------------------------------------------------#
  57. # Increasing cron value
  58. increase_user_value $user '$U_CRON_JOBS'
  59. # Restarting crond
  60. $BIN/v-restart-cron
  61. check_result $? "Cron restart failed" >/dev/null
  62. # Logging
  63. log_history "added cron job $job"
  64. log_event "$OK" "$ARGUMENTS"
  65. exit