I tried to use at
from within a script of mine and it prints:
warning: commands will be executed using /bin/sh
How would I use different shell if I wanted?
Under Linux, at
always warns you that it will execute the specified commands with /bin/sh
, rather than your favorite shell. You cannot suppress this message, it's hard-coded in the source code.
The command you pass is interpreted by /bin/sh
. This command can be the path to a script if you like; then /bin/sh
will execute the script program, causing the script's interpreter to be launched and to interpret the script. The language of the script is completely independent of the program that starts it. So if for example you want to execute a bash script (i.e. a script that begins with #!/bin/bash
), just pass the path to the script to at
(echo /path/to/script | at 3:42
) and ignore the irrelevant message.
at
. I said to pass the path to the script, i.e. echo /path/to/script | at …
.
Commented
Dec 11, 2021 at 8:57
you can achieve running it from a different shell changing the script shebang. Some typical shebang lines:
#!/bin/sh — Execute the file using sh, the Bourne shell, or a compatible shell #!/bin/csh -f — Execute the file using csh, the C shell, #!/usr/bin/perl -T — Execute using Perl with the option for taint checks #!/usr/bin/php — Execute the file using the PHP command line interpreter #!/usr/bin/python -O — Execute using Python with optimizations to code #!/usr/bin/ruby — Execute using Ruby
To run a script at given times I suggest you adding a cronjob
Example:
The following line makes the user program test.pl—ostensibly a Perl script—run every two hours, at midnight, 2am, 4am, 6am, 8am, and so on:
0 */2 * * * /home/username/test.pl
#!/usr/bin/perl
is already the shebang in my script but I get the warning
One option is to make the command itself a script send to bash (as an example of a different shell):
command:
<<'EOD' at now
<<'BASH' /bin/bash -s -- test
echo "$BASH ::" "$@"
BASH
EOD
output:
warning: commands will be executed using /bin/sh
job 148 at Sat Feb 13 00:24:00 2021
at(1) then will execute with /bin/sh
the inner command:
<<'BASH' /bin/bash -s -- test
echo "$BASH ::" "$@"
BASH
Which is executing the echo line with the the bash-shell specific $BASH
variable and one positional argument test with /bin/bash
(as an example for a different shell).
That's it.
To suppress the hard-coded error message, prefix the complex command with:
2> >(sed '/warning: commands will be executed using \/bin\/sh/d' )
as it will remove the warning about execution with /bin/sh
, and only that warning and not other ones.
command:
2> >(sed '/warning: commands will be executed using \/bin\/sh/d' ) \
<<'EOD' at now
<<'BASH' /bin/bash -s -- test
echo "$BASH ::" "$@"
BASH
EOD
output:
job 150 at Sat Feb 13 00:26:00 2021
The at command will use /bin/sh even if you added #!/bin/bash to the script. To make it use bash you need to do something like:
echo script.sh | at now+x minutes
In that case bash will be used if you included #!/bin/bash in the top of the script.
Also note that /bin/sh if symlinked to /bin/bash for example, it will not behave exactly like bash if called that way. Thus if your script uses bash specific features it would have errors if run using at this way:
at -f script.sh now+x minutes
These errors will only show up if you check your (local) email. Typically /var/mail/username if something like sendmail is present. If there is no way to send email at will discard the output and erros.
Easy to miss, if you test the script by hand it works great, when run using at it may have errors. Because in the latter case it would be run using /bin/sh.