Home > OS >  specify time zone in cronR macosx
specify time zone in cronR macosx

Time:03-27

I have a simple script to run a model at a set time everyday using cronR. Does anyone know how to specify the time zone in which the cron job is implemented.

Here is my simple script

# create the command to execute the R script
exec_model <- cron_rscript(rscript = path_model, cmd = file.path('/usr/local/bin/Rscript'),  log_append = FALSE)

# add the command and specify the days/times to start
cron_add(command = exec_model, frequency = 'daily', at = '04:31', 
         id = 'model', description = 'execute model')

the cron_add function allows me to specify the time - e.g. 04:31 - I assume this time is the time my system's clock? I would like to specify this to be 04:31 UTC and independent of my local time.

CodePudding user response:

According to the documentation of ?cron_add, there is a env argument

cron_add( command, frequency = "daily", at, days_of_month, days_of_week, months, id, tags = "", description = "", dry_run = FALSE, user = "", ask = TRUE, env = character() )

which can take the Sys.getenv()

env - Named character; set environment variables for a cron job. Specify ‘Sys.getenv()‘ to inherit the variables from the current R session

Therefore, we may change the TZ with

Sys.setenv(TZ="UTC")

and specify the env as a named vector (after extracting the TZ with Sys.getenv("TZ")) to run the cron job

cron_add(command = exec_model, frequency = 'daily', at = '04:31', 
         id = 'model', description = 'execute model', 
   env = c(TZ = Sys.getenv("TZ"))
  • Related