I have a zsh script that I would like to run via anacron every week.
It runs fine from an interactive bash shell, but when run by anacron, I am getting command not found errors and it looks like it is ignoring the shebang line.
The actual /etc/cron.weekly/ script is:
#!/bin/sh
[ -x SCRIPT ] || exit 1
su NORMAL_USER SCRIPT
And the top line of the actual script is #!/bin/zsh
How can I get anacron to honor the shebang? Would changing NORMAL_USER's login shell to zsh accomplish this?
Why it doesn't Work:
The reason that it doesn't work is that su requires that you run the command from a terminal, and cron does not provide a terminal.
Simple way to do it is using Vixie Cron:
If you didn't mind using Vixie Cron I think the simplest thing to do would be to put the script in for that user by running the following as that user:
Then use
0 0 * * 0
as the time.If you want to use Anacron as a User:
Anacron is helpful if you want to make sure the job doesn't get missed if the machine was off. To run as different user, specify a different anacron tab file and spool dir, put the following in the system cron to run as the specified user,
anacron -t /home/foo/etc/anacrontab -S /home/foo/var/spool/
and then edit that tab file to run the script.There is also a mini-howto, but this looks more complicated.
You will have to test these though to verify. Lastly, remember this for future testing
run-parts --test /etc/cron.weekly
The shebang should take care of it. The first thing I look at when scripts don't run correctly under
cron
is environment variables such as$PATH
. The$PATH
forcron
is much simpler than that of a user. You can set a$PATH
in thecron
script.You can also hard-code the full path to all executables in your script.
If
zsh
is not at/bin/zsh
you should get an invalid interpreter message, but you said it's running fine from a Bash prompt, so I doubt that's the problem.Changing the user's login shell to zsh makes this work. I had been wanting to try out zsh as my login shell for awhile anyway. Nothing like necessity to give you a push to do something.