I want to execute a Bash function at a scheduled time. I think the right tool for this is the at
command.
However, it doesn't seem to be working.
function stupid () {
date
}
export -f stupid
cat << EOM | at now + 1 minute
stupid
EOM
After waiting the minute, I check my mail and this is the error I see:
sh: line 41: syntax error near unexpected token `=\(\)\ {\ \ date"
"}'
sh: line 41: `"}; export BASH_FUNC_stupid()'
I don't understand what's going wrong here, especially since I know the function works.
$ stupid
Fri May 29 21:05:38 UTC 2015
Looking at the error, I think the incorrect shell is being used to execute the function (sh
as opposed to bash
), but if I check $SHELL
I see it points to /bin/bash
, and the man page for at
says:
$ echo $SHELL
/bin/bash
$ man at
...
SHELL 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.
So Bash should be the shell running my function.
What going on here? Is there a way to get my Bash function to run with at
?
GNU bash, version 4.1.2(1)-release
, which predates the shellshock fixes discussed in that thread by a few years. So perhaps it's not exactly the same issue. I should try with the latest release of Bash to see if this has been fixed. – Nick Chammas May 29 '15 at 22:224.3.30(1)-release
and try the original snippet from my question, I now get:/bin/bash: line 1: stupid: command not found
. So now it looks likebash
and notsh
is running, which is good, but the function is no longer accessible. – Nick Chammas May 29 '15 at 22:49bash -c 'stupid'
toat
instead of juststupid
, I get the original syntax error message withsh:
. I'm lost now.¯\_(ツ)_/¯
– Nick Chammas May 29 '15 at 22:51