R-Enthusiast
R-Enthusiast

Reputation: 350

Running R script from crontab gives different results than from command line

I am using a machine that employs Ubuntu 10.04.4 (lucid) and R version 2.15.3.

I run this R script using R CMD BATCH test.time.R from the command line, and it works fine:

format(Sys.time(), " %H")
[1] " 15"

format(Sys.time(), " %H") >= 12
[1] TRUE

But when I run */1 * * * * R CMD BATCH test.time.R from crontab, it doesn't:

format(Sys.time(), " %H")
[1] " 15"

format(Sys.time(), " %H") >= 12
[1] FALSE

Any thoughts?

This is the only machine (out of four) where this is the case (other machines read it fine from crontab).

I'd appreciate any thoughts. Thanks!

Upvotes: 1

Views: 303

Answers (1)

GSee
GSee

Reputation: 49810

Don't compare a string to a number if you don't have to. If you removed the space from the format pattern (i.e. made it "%H" instead of " %H"), it should work. However, you don't have to introduce strings in this case; you can get the hour as a number by converting to POSIXlt and extracting the hour component. Try this:

as.POSIXlt(Sys.time())$hour > 12

Upvotes: 2

Related Questions