I have some commands in my .profile
that I want to invoke from my crontab
.
For example, if I have,
alias notify-me="~/bin/notify.pl -u user1"
alias notify-team="~/bin/notify.pl -u user1 user2 user3 user4 ...."
I'd like to just invoke the alias
0 11 * * * notify-team
so if the list in my .profile
is updated, I don't have to update the crontab
, too. However, I don't seem to be able to use aliases in my crontab
. Is there a work around?
I tried the suggestions here to set up the environment (e.g. /bin/bash -lc and the script-wrapper script). It seems like that works for scripts but not aliases.
Any ideas?
From the manpage regarding aliases:
So try using
shopt -s expand_aliases
at the start of your sourcing script. This should let you use Warner's suggestion.bash -c with "source" should work:
this might also work. the period means "source"
As Chris identified, the default shell option for non-interactive shells is to not expand aliases. Here's a solution I've found to work.
Write a script, enable the shell option, and source your aliases. Be particularly aware that
.bashrc
is sourced at execution, which is why it has to be sourced again after enabling expand_aliases.My apologies for the initially incorrect recommendation. This was more obscure than I initially expected it to be.
Script:
I like artifex's idea of grabbing the alias from the file and then reusing it, since I couldn't find a way to expand/reuse aliases directly. (Other solutions still required another script.)
So I wrote this function and put it in my .profile:
Then I was able to use it in my crontab for various aliases like this:
etc.
Yay, code reuse! Thanks, all.
Really the simplest thing you could do would be to create
~/bin/notify-me with
~/bin/notify-team with
shell aliases are complicated to maintain and to integrate into other systems, as you've seen. the best thing to do is to turn them into full fledged commands that won't have funny environment issues.
bash -ic "notify-team" is best option. It makes alias available in crontab. You may further add a & for background as we are using a 'interactive' shell with -i , but in reality not interacting with it.
works for me