2

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?

Kyle Brandt
  • 83,619
  • 74
  • 305
  • 448
Ryan Ahearn
  • 317
  • 2
  • 10

3 Answers3

2

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

Kyle Brandt
  • 83,619
  • 74
  • 305
  • 448
1

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 for cron is much simpler than that of a user. You can set a $PATH in the cron 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.

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • Currently the most portable shebang is `#!/usr/bin/env zsh` . This works regardless of where `zsh` resides, in most distros. – Jeff Learman Jun 06 '23 at 15:58
0

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.

Ryan Ahearn
  • 317
  • 2
  • 10
  • Perhaps this works for `anachron`. Generally, `chron` ignores the user's login shell. This behavior has been pretty consistent for me, from the 80's to now. I have never seen the login shell be used for cron jobs, in probably a dozen different forms of *nix including System III, System V, BSD, AIX, RHEL, and Ubuntu. – Jeff Learman Jun 06 '23 at 16:11