v-change-cron-job 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/bash
  2. # info: change cron job
  3. # options: USER JOB MIN HOUR DAY MONTH WDAY COMMAND
  4. #
  5. # The function is used for changing existing job. It fully replace job
  6. # parameters with new one but with same id.
  7. #----------------------------------------------------------#
  8. # Variable&Function #
  9. #----------------------------------------------------------#
  10. # Argument definition
  11. user=$1
  12. job=$2
  13. min=$3
  14. hour=$4
  15. day=$5
  16. month=$6
  17. wday=$7
  18. command=$8
  19. # Includes
  20. source $VESTA/func/main.sh
  21. source $VESTA/conf/vesta.conf
  22. #----------------------------------------------------------#
  23. # Verifications #
  24. #----------------------------------------------------------#
  25. check_args '7' "$#" 'USER JOB MIN HOUR DAY MONTH WDAY COMMAND'
  26. is_format_valid 'user' 'job' 'min' 'hour' 'day' 'month' 'wday' 'command'
  27. is_system_enabled "$CRON_SYSTEM" 'CRON_SYSTEM'
  28. is_object_valid 'user' 'USER' "$user"
  29. is_object_unsuspended 'user' 'USER' "$user"
  30. is_object_valid 'cron' 'JOB' "$job"
  31. is_object_unsuspended 'cron' 'JOB' "$job"
  32. #----------------------------------------------------------#
  33. # Action #
  34. #----------------------------------------------------------#
  35. # Generating timestamp
  36. time_n_date=$(date +'%T %F')
  37. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  38. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  39. # Concatenating cron string
  40. command=$(echo $command | sed -e "s/'/%quote%/g")
  41. str="JOB='$job' MIN='$min' HOUR='$hour' DAY='$day' MONTH='$month' WDAY='$wday'"
  42. str="$str CMD='$command' SUSPENDED='no' TIME='$time' DATE='$date'"
  43. # Deleting old job
  44. sed -i "/JOB='$job' /d" $USER_DATA/cron.conf
  45. # Adding new
  46. echo "$str" >> $USER_DATA/cron.conf
  47. # Sorting jobs by id
  48. sort_cron_jobs
  49. # Sync system cron with user
  50. sync_cron_jobs
  51. #----------------------------------------------------------#
  52. # Vesta #
  53. #----------------------------------------------------------#
  54. # Restarting crond
  55. $BIN/v-restart-cron
  56. check_result $? "Cron restart failed" >/dev/null
  57. # Logging
  58. log_history "changed cron job $job"
  59. log_event "$OK" "$ARGUMENTS"
  60. exit