1

I would like cron to run a script from a specific shell (Zsh). I thought the following would work:

00 02 * * * exec zsh; /path/to/script.sh

but apparently it doesn't, why?

This also made me wonder, how do I find out what shell and init scripts does cron run first prior to running the entry in crontab?

slm
  • 369,824
Josh
  • 1,744

2 Answers2

7

How about:

00 02 * * * exec /usr/bin/zsh /path/to/script.sh

That will tell zsh to run the script. Now you want it to be run in zsh doesn't matter what, just add the shebang in the start:

#!/usr/bin/zsh
the_rest
Braiam
  • 35,991
  • 1
    why is everybody using an 'exec' before the shell command in cron? Simply using '/usr/bin/zsh /path/to/script.sh' is sufficient and exec is really unnecessary. – mdpc Apr 28 '14 at 01:36
3

Cron has several enviromental variables configured in /etc/crontab, specifically SHELL and PATH. The default value for SHELL is /bin/sh. So unless this is changed or otherwise specified in a script, cron will execute commands using sh.

Creek
  • 5,062
  • NOTE: Some versions of cron allow specification of some global variables. To be sure checkout the man pages for cron, crond, and crontab for your particular distribution. – mdpc Apr 28 '14 at 01:37