Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Thanks, all for the useful info. Couple possible solutions here including my own. I ultimately modified artifex's original idea to something more useful for my situation. Chris provided the necessary info to do it with separate scripts. –  Keith Bentrup Mar 12 '10 at 23:22
add comment

5 Answers

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.

share|improve this answer
    
This is on for me on CentOS5 by default. Cool note though. –  Warner Mar 4 '10 at 21:17
    
Probably not in a script, though. Try creating a test script that outputs shopt expand_aliases. I'm betting it's off. It is in my RHEL 5.3 box. –  Christopher Karel Mar 4 '10 at 22:09
    
You hit the nail on the head. +1 –  Warner Mar 4 '10 at 22:32
    
+1 Thx for pointing this out. It gets me most of the way to a solution, but I still end up having to create another file. I was hoping to do it without that. –  Keith Bentrup Mar 6 '10 at 3:33
    
Keith, I had the same issue but doing eval 'foo' in crontab did the trick. –  Artem Russakovskii Jun 19 '10 at 0:56
add comment

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
share|improve this answer
add comment

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
share|improve this answer
    
Thx, but I've tried that already. Can you confirm that it works on your system? I've also tried /bin/bash -lc source ~/.profile; notify-team and other variations. Also I prefer not to have a separate script for each alias. That defeats the purpose of avoiding duplication. –  Keith Bentrup Mar 4 '10 at 19:03
    
I should have included the error from crontab: "command not found" on every variation that I've tried. –  Keith Bentrup Mar 4 '10 at 19:05
    
Edited with a tested recommendation. –  Warner Mar 4 '10 at 22:35
    
Thx for the updated example. I think if you add -l to your 1st line you can get rid of your 3rd. –  Keith Bentrup Mar 6 '10 at 4:25
    
Ah, good call-- login shell would have expand_aliases enabled. –  Warner Mar 6 '10 at 4:32
show 1 more comment

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.

share|improve this answer
    
Thanks for suggestion, but I never thought aliases were hard to maintain, and I'd rather maintain 1 file of aliases then maintain lots of wrapper files. The problem was executing the aliases in a cron job. –  Keith Bentrup Mar 6 '10 at 6:30
add comment

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.

share|improve this answer
    
that is pure evil. –  Justin Mar 6 '10 at 6:01
    
If I could use the alias directly, I would. That's the point of the question. You're welcome to propose another solution, but I'll take this solution over lots of 2-3 line files cluttering up a directory - which is what will happen as I add additional notification schemes or decide to reuse some of my other aliases. –  Keith Bentrup Mar 6 '10 at 6:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.