0

I am having a function written inside bashrc like -

function build_image ()
{
 ...
}

This function will trigger compilation of the source code.

Now I need to trigger this image building/compilation process at midnight today. So I make use of at command.

So I ran like -

-bash-4.4 $at midnight
warning: commands will be executed using /bin/sh
at> build_image
at> <EOT>
job 4 at Thu Feb 11 00:00:00 2021

Looks like the shell that is used by at is sh. How do I instruct at command to use bash shell so that my alias'es and functions work?

Darshan L
  • 279

2 Answers2

1

From the manual page:

The value of the SHELL environment variable at the time of at invocation will determine which shell is used to execute the at job commands. If SHELL is unset when at is invoked, the user's login shell will be used.

However, .bashrc is only sourced when your shell is interactive. In the case of at, you need to set the BASH_ENV variable or use a different mechanism (such as implementing the function as a separate shell script). See the startup files section in the Bash manual.

berndbausch
  • 3,557
0

at is designed to use /bin/sh instead of /bin/bash, so it prepends a code prologue that you can see with at -c {jobnumber}.  If you want to insert stuff for bash and use it, here is a simple function:

at_with_bash_alias_and_functions(){
  ( 
    echo "exec /bin/bash <<END"
    alias
    typeset -f
    cat
    echo END
  ) | sed 's/\$/\\$/g' | at "$@"
}

Show alias and functions are present:

at_with_bash_alias_and_functions now <<< "alias;typeset -f"

Limits: inlining may lose syntax due to shell substitution; it doesn't work exactly like at.  Verify your syntax with exec cat instead of exec bash.