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
  • 363,520
  • 117
  • 767
  • 871
Josh
  • 1,694
  • 3
  • 16
  • 32
  • 4
    Did you add #!/bin/zsh at the beginning? – Ghassan Apr 27 '14 at 19:52
  • Is `script.sh` a zsh script or do you want zsh to launch an `sh` script? What's the `exec` supposed to do? – terdon Apr 27 '14 at 21:24
  • Thanks @Ghassan. I didn't have a working shebang, partly because of I was suffering from the same problem reported [here](http://unix.stackexchange.com/questions/126812/path-independent-shebangs). I should be able to fix this now. – Josh Apr 28 '14 at 12:24
  • @Josh You're welcome dude. Good luck. – Ghassan Apr 28 '14 at 12:46

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,380
  • 25
  • 108
  • 167
  • 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,002
  • 1
  • 22
  • 33
  • 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