Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
4  
Did you add #!/bin/zsh at the beginning? –  Ghassan Apr 27 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 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 at 12:24
    
@Josh You're welcome dude. Good luck. –  Ghassan Apr 28 at 12:46

2 Answers 2

up vote 3 down vote accepted

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
share|improve this answer
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 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.

share|improve this answer
    
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 at 1:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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