Script produces different result when executed by Bash than by cron

前端 未结 3 1992
不知归路
不知归路 2021-01-18 20:36

Please consider following crontab (root):

SHELL=/bin/bash
...
...
0 */3 * * * /var/maintenance/raid.sh

And the bash script /var/maint

相关标签:
3条回答
  • 2021-01-18 21:00

    Before running your script as a cron job, you can test it with no environment variables using env -i

    env -i /var/maintenance/raid.sh
    
    0 讨论(0)
  • 2021-01-18 21:03

    As indicated in the comments, do use full paths on crontab scripts, because crontab does have different environment variables than the normal user (root in this case).

    In your case, instead of mdadm, /sbin/mdadm makes it.

    How to get the full path of a command? Using the command command -v:

    $ command -v rm
    /bin/rm
    
    0 讨论(0)
  • 2021-01-18 21:18

    cron tasks run in a shell that is started without your login scripts being run, which set up paths, environment variables etc.

    When building cron tasks, prefer things like absolute paths and explicit options etc

    0 讨论(0)
提交回复
热议问题