Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test things in crontab

This keeps happening to me all the time: 1) I write a script(ruby, shell, etc). 2) run it, it works. 3) put it in crontab so it runs in a few minutes so I know it runs from there. 4) It doesnt, no error trace, back to step 2 or 3 a 1000 times.

When I ruby script fails in crontab, I can't really know why it fails cause when I pipe output like this:

ruby script.rb >& /path/to/output

I sorta get the output of the script, but I don't get any of the errors from it and I don't get the errors coming from bash (like if ruby is not found or file isn't there)

I have no idea what environmental variables are set and whether or not it's a problem. Turns out that to run a ruby script from crontab you have to export a ton of environment variables.

Is there a way for me to just have crontab run a script as if I ran it myself from my terminal?

When debugging, I have to reset the timer and go back to waiting. Very time consuming.

How to test things in crontab better or avoid these problems?

like image 992
ulver Avatar asked Aug 12 '09 12:08

ulver


People also ask

What does 0 * * * * mean in crontab?

0 * * * * -this means the cron will run always when the minutes are 0 (so hourly) 0 1 * * * - this means the cron will run always at 1 o'clock. * 1 * * * - this means the cron will run each minute when the hour is 1. So 1:00 , 1:01 , ... 1:59 .


1 Answers

"Is there a way for me to just have crontab run a script as if I ran it myself from my terminal?"

Yes:

bash -li -c /path/to/script

From the man page:

[vindaloo:pgl]:~/p/test $ man bash | grep -A2 -m1 -- -i
   -i        If the -i option is present, the shell is interactive.
   -l        Make bash act as if it had been invoked as a login shell (see
             INVOCATION below).
like image 90
pgl Avatar answered Sep 28 '22 00:09

pgl