[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Weird shell script behavior in a cron job



On Wed, Aug 30, 2017 at 09:32:37PM +0300, Reco wrote:
> > > #!
> 
> A curious shebang.

> > Why would the behavior be any different? Could it be that cron is running it
> > an entirely different shell, that doesn't understand the "if" statement?
> 
> Presumably your script runs via /bin/bash in interactive mode, and via
> /bin/sh (should be /bin/dash) if run by cron.

Yes, exactly this.  The shebang is malformed, so the kernel cannot
execute the script directly.  When a shell tries to run this script,
the kernel will return ENOEXEC.  The shell sees this, and forks a
child of itself to be the interpreter.

>From an interactive bash shell, therefore, your script would be run by
bash.

>From crontab, each line is executed by /bin/sh, so your script would
end up being run by another instance of /bin/sh.

This is why it's vitally important to put the correct shebnang on every
script you run.  If you don't, you either get direct failures if you're
lucky, or indeterminate behavior if you are not.


Reply to: