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?

  • 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. 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

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:

  • 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 1:36

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.

  • 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 1:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.