6

I have a script containing:

#!/bin/bash
printenv

When I run it from the command line:

env testscript.sh
bash testscript.sh
sh testscript.sh

every time, it outputs SHELL=/bin/bash. However, when it is run from the cron, it always outputs SHELL=/bin/sh. Why is this? How can I make cron apply the shebang?

I already checked the cron PATH; it does include /bin.

  • Shell scripts in general do not change environment variables so the value of SHELL will not change. First check to see if /bin/sh is a link to bash - it is on many systems. If /bin/sh is something else link dash, try using the bash specific commands you need and see if they fail. Finally, you can use something like '[ -z $BASH ] && { exec bash "$0" "$@" || exit; }' as the first line in your script to make sure you're running it under bash. – doneal24 Jul 15 '15 at 15:01
  • What distro is this? – ILMostro_7 Jul 15 '15 at 18:27
  • 3
    Read man 5 crontab. cron sets environment variables SHELL, LOGNAME, HOME and PATH. – fd0 Jul 15 '15 at 18:45
5

The shebang is working and cron has nothing to do with that. When a file is executed, if that file's content begins with #!, the kernel executes the file specified on the #! line and passes it the original file as an argument.

Your problem is that you seem to believe that SHELL in a shell script reflects the shell that is executing the script. This is not the case. In fact, in most contexts, SHELL means the user's prefered interactive shell, it is meant for applications such as terminal emulator to decide which shell to execute. In cron, SHELL is the variable that tells cron what program to use to run the crontab entries (the part of the lines after the time indications).

Shells do not set the SHELL variable unless it is not set when they start.

The fact that SHELL is /bin/sh is very probably irrelevant. Your script has a #!/bin/bash line, so it's executed by bash. If you want to convince yourself, add ps $$ in the script to make ps show information about the shell executing the script.

1

Bash Reference Manual says:

SHELL - The full pathname to the shell is kept in this environment variable. If it is not set when the shell starts, Bash assigns to it the full pathname of the current user’s login shell.

man 5 crontab says:

Several environment variables are set up automatically by the cron(8) daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner. PATH is set to "/usr/bin:/bin". HOME, SHELL, and PATH may be overridden by settings in the crontab

So the SHELL variable is set when the Bash start.

Try SHELL=/bin/awesome/shell bash testcript.sh. You should see SHELL=/bin/awesome/shell

Shebang works. You have a documented behaviour:)

-1

Change the shebang to

#!/bin/env bash

Or execute crontab -e to have the script run with your own environment variables, as cron's own are different.


As a side note, according to this post on askubuntu, you need to give your cron a PATH at the top of the script:

PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# rest of script follows

However, given that you said PATH seems to have the proper search directories,you can rule that out.

  • Benubird is not experiencing behaviour that can be caused by a wrong PATH or a wrong path to the shell. – Wieland Jul 15 '15 at 18:36
  • Yah, I saw that bit after I added the part about the PATH; decided to leave it there, nonetheless. This question has multiple versions floating around on here. – ILMostro_7 Jul 15 '15 at 18:39
  • @Wieland, if his PATH is fine, then env bash should work, no? Ultimately, the question is "How can I make cron apply the shebang"! – ILMostro_7 Jul 15 '15 at 18:46
  • 1
    Yes, but the path to his bash is already correct, otherwise there would be no output at all. – Wieland Jul 15 '15 at 18:47
  • Your downvotes sting like soap in my eyes – ILMostro_7 Jul 15 '15 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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