1

I've read the answers to this question, but still don't understand what to do:

How to use my aliases in my crontab?

I am logged onto my Ubuntu server as root. I have the following command in my .profile:

alias test-alias="echo test"

I have the following command in my crontab file:

11 9 * * *      source /root/.profile; test-alias > /root/tmp.output 2>&1

When this command runs, the only output present in tmp.output is:

/bin/sh: 1: test-alias: not found

What am I doing wrong here? How can I use my test-alias in my crontab file? I want to use the alias directly in the command, I don't want to create additional scripts to run the alias.

flyingL123
  • 235
  • 3
  • 12
  • What are the (failed?) results of you trying the answers in the question you linked? Any answers to this question are just going to be copies of the other one. – Hyppy Feb 05 '15 at 14:26
  • I tried making the crontab command `shopt -s expand_aliases; source /root/.profile; test-alias > /root/tmp.output 2>&1` but that did not change anything. Same result. I thought that's what the highest voted answer was saying to do. What am I doing wrong there? – flyingL123 Feb 05 '15 at 14:29

1 Answers1

1

Although it's not the prettiest solution and although I would suggest you against using it, what you can do is:

11 9 * * *      bash -ic "test-alias > /root/tmp.output 2>&1"

This will run bash as interactive shell (-i) and will thus read bashrc. To make sure .profile is sourced, you need to have this block in your .bashrc:

[ -f ~/.profile ] && source ~/.profile

Note that this kind of running cron jobs or writing scripts is a really bad practice, and you should try and avoid it.

Jakov Sosic
  • 5,157
  • 3
  • 22
  • 33
  • Thank you. Why is it bad practice? What would be a better way to do it? – flyingL123 Feb 05 '15 at 14:38
  • Because one day someone can move cron jobs to another machine where they will fail (because of missing aliases), and also it's uncommon way of doing things so many people administering machines after you will have a head scratching sessions while trying to figure out what's going on. Cron jobs should be written in a way that they are self-sustained and environment independent. So better way would be to write a shell script. But if it's only for your personal use, then it's ok. – Jakov Sosic Feb 05 '15 at 14:48
  • What does the `-c` option do in your solution? I see that it means `commands are read from the first non-option argument command_string`, but I don't understand what that actually means or why it is necessary. – flyingL123 Feb 05 '15 at 14:52