10

I write a script and set it as a cron job. But due to a difference of environment variables it doesn't work as it should be.

In that case I change a little bit with crontab -e and set a cron job time closest minute, and wait next minute to come to show the result. I feel this is a totally absurd approach, but I don't know better way to do it.

If there is a way to run a script as if it is called inside cron job, I'm going to use it.

Does anyone know how to do it?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
ironsand
  • 5,085
  • 12
  • 50
  • 73
  • 1
    You could grab the environment from cron by scheduling `env` to run... – derobert Mar 26 '14 at 00:54
  • 1
    Related (1): [executing a sh script from the cron](http://unix.stackexchange.com/questions/6790/executing-a-sh-script-from-the-cron/6794#6794) – slm Mar 26 '14 at 01:59
  • Related (2): [Frustrating issue where neither cron nor su -c runs my job (permissions?)](http://unix.stackexchange.com/questions/31117/frustrating-issue-where-neither-cron-nor-su-c-runs-my-job-permissions/31120#31120) – slm Mar 26 '14 at 02:01
  • Related (3): [Why does Crontab give me an error with my PHP script when running it from the terminal does not?](http://unix.stackexchange.com/questions/9880/why-does-crontab-give-me-an-error-with-my-php-script-when-running-it-from-the-te/9924#9924) – slm Mar 26 '14 at 02:02
  • If you want to debug `cron`, just redirect the error output, run this with it `your_script.sh 2>/path/to/logfile`, that should help you understand what's going on. – terdon Mar 26 '14 at 02:18

3 Answers3

5

Here is how to do the other way around: forcing cron execution to use your login environment:

bash -lc "your_command"

From the bash manual:

-c string     If the -c option is present, then commands are read from string.
              If there are arguments after the string, they are assigned to the
               positional parameters, starting with $0.
-l            Make bash act as if it had been invoked as a login shell
               (see INVOCATION below).

INVOCATION (a bit stripped):

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

To known more:

Ouki
  • 5,842
  • 4
  • 23
  • 31
2

Another way would be:

/usr/bin/env --ignore-environment your command

From the manpage of env:

-i, --ignore-environment

start with an empty environment

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Bogus-bill
  • 21
  • 1
-1

A better way is to use at.

Here are some examples

 echo $PWD/script.sh | at now

or

 echo "reboot" | at 5:00

or

 echo "mail -s test user@host" | at now + 1 hour

Here are some date/time Examples