Cron – Change the shell for cron job

cronshellunixzsh

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?

Best Answer

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:

crontab -e 

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

Related Topic