27

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?

7 Answers7

12

From the manpage regarding aliases:

Note aliases are not expanded by default in  non-interactive  shell,
and  it  can  be  enabled  by  setting  the 'expand_aliases'
shell option using shopt.

So try using shopt -s expand_aliases at the start of your sourcing script. This should let you use Warner's suggestion.

10

bash -c with "source" should work:

0 11 * * * bash -c "source .profile && notify-team"

this might also work. the period means "source"

0 11 * * * . .profile && notify-team
Collin Anderson
  • 491
  • 2
  • 6
  • 14
7

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:

#!/bin/bash
shopt -s expand_aliases
source /home/wmoore/.bashrc
notify-team
Warner
  • 24,174
  • 2
  • 63
  • 69
1

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:

grab-alias () { sed -n '/alias\s*'$1'\s*=/ {s/[^'\'']*.//;s/'\''\s*$//p}' /home/bentrupk/.profile; }

Then I was able to use it in my crontab for various aliases like this:

0 11 * * * /bin/bash -lc 'x=`grab-alias notify-team`; $x'
0 7 * * * /bin/bash -lc 'x=`grab-alias notify-me`; $x'

etc.

Yay, code reuse! Thanks, all.

1

Really the simplest thing you could do would be to create

~/bin/notify-me with

#!/bin/sh
~/bin/notify.pl -u user1

~/bin/notify-team with

#!/bin/sh
~/bin/notify.pl -u user1 user2 user3 user4 ....

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.

Justin
  • 3,906
0

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.

-3
0 11 * * * bash -ic "notify-team" > /log/file

works for me