R/cron_clear.R

Defines functions cron_clear

Documented in cron_clear

#' @title Clear all cron jobs
#' @description Clear all cron jobs
#' @param ask Boolean; ask before removal?
#' @param user The user whose crontab we are clearing.
#' @export
#' @examples
#' \dontshow{if(interactive())
#' \{
#' }
#' f   <- system.file(package = "cronR", "extdata", "helloworld.R")
#' cmd <- cron_rscript(f)
#' cron_add(command = cmd, frequency = 'minutely', id = 'test1', description = 'My process 1')
#' cron_add(command = cmd, frequency = 'daily', at="7AM", id = 'test2', description = 'My process 2')
#' cron_njobs()
#' 
#' cron_ls()
#' cron_clear(ask=TRUE)
#' cron_ls()
#' \dontshow{
#' \}
#' }
cron_clear <- function(ask=TRUE, user="") {
  if (ask) {
    if (user == "")
      cat( sep="", "Are you sure you want to clear all your cron jobs? [y/n]: ")
    else
      cat( sep="", "Are you sure you want to clear all cron jobs for '",
        user, "'? [y/n]: ")
    input <- tolower(scan(what=character(), n=1, quiet=TRUE))
    if (input != "y") {
      ok <- 0
      message("No action taken.")
      return (invisible(ok))
    }
  }
  if (user == "")
    ok <- system("crontab -r")
  else
    ok <- system(sprintf("crontab -u %s -r", user))
  if (ok == 0)
    message("Crontab cleared.")
  return (invisible(ok))
}

Try the cronR package in your browser

Any scripts or data that you put into this service are public.

cronR documentation built on Jan. 9, 2023, 5:10 p.m.