How to get CRON to call in the correct PATHs

前端 未结 15 1127
时光取名叫无心
时光取名叫无心 2020-11-22 06:07

I\'m trying to get cron to call in the correct PATHs. When I run a Python script from shell the script runs fine as it uses the PATHs set in bashrc but when I use cron all t

15条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 06:53

    The simplest workaround I've found looks like this:

    * * * * * root su -l -c command
    

    This example invokes su as root user and starts the shell with the user's full environment, including $PATH, set as if they were logged in. It works the same on different distros, is more reliable than sourcing .bashrc (which hasn't worked for me) and avoids hardcoding specific paths which can be a problem if you're providing an example or setup tool and don't know what distro or file layout on the user's system.

    You can also specify the username after su if you want a different user than root, but you should probably leave the root parameter before su command since this ensures su has sufficient privileges to switch to any user you specify.

提交回复
热议问题