Linux - 切换到“cron”用户

Max*_*ams 1 linux cron crontab

有时我遇到 cron 作业未运行的问题,并且诊断原因可能很困难。有用的是切换到与运行 cron 作业相同的用户,这样我就可以查看是否存在权限问题或其他问题。谁能告诉我如何做到这一点?我的 linux 版本来自uname -r,是2.6.32-36-generic

干杯,最大

编辑 - 顺便说一句,我知道如何切换用户!我的问题更多是关于找出要切换到哪个用户。

编辑 - 在我当前的问题中,我在 crontab 中有一行,当我将它粘贴到终端(在任何文件夹中)时可以工作,但是从 cron 运行时失败。我正在努力找出原因。我给出了使用的任何文件的完整路径。

42 13 * * * cd /path/to/my/working/folder && /path/to/my/working/folder/script/runner 'MusicService.update_cached_data' -e staging
Run Code Online (Sandbox Code Playgroud)

(42 13 次是因为那是我最后一次测试它)

ter*_*don 5

Cronjobs 以任何用户设置 crontab 的身份运行。他们不作为特殊用户运行。通常,如果您知道 crontab 存在,您就知道是谁设置的。您可以通过运行查看当前用户的 crontabs

crontab -l
Run Code Online (Sandbox Code Playgroud)

您可以在目录中找到特定于用户的 crontab /var/spool/cron/crontabs/。每个创建 crontab 的用户都会在该目录中拥有一个文件(其名称是用户名)。默认情况下,这些文件只能由创建它们的用户读取,因此您不能在不cat使用他们的权限的情况下访问它们。

这个小脚本将列出每个用户的所有 cron 命令:

for u in $(find /var/spool/cron/crontabs/ -type f); do 
 user=`basename $u`; 
 echo "------- $user ----"; 
 crontab -u  $user -l | grep -v "#"; 
done
Run Code Online (Sandbox Code Playgroud)

这将列出 .crontab 文件的每个用户的所有 cronjobs /var/spool/cron/crontabs/

最后,您还有系统范围的 crontab,/etc/crontab您可以通过运行cat /etc/crontab.


为了回答您的评论,如果您想加载在给定文件中定义的特定变量,您可以source在您的crontab:

42 13 * * * . ~/.bashrc && cd /path/to/my/working/folder && /path/to/my/working/folder/script/runner 'MusicService.update_cached_data' -e staging
Run Code Online (Sandbox Code Playgroud)

也就是说.,后跟要加载的文件。


最后,以下是一些相关信息man 5 crontab

   An active line in a crontab will be either  an  envi?
   ronment  setting or a cron command.  The crontab file
   is parsed from top to bottom, so any environment set?
   tings  will  affect only the cron commands below them
   in the file.  An environment setting is of the form,

       name = value

   where  the  spaces  around  the  equal-sign  (=)  are
   optional,  and  any  subsequent non-leading spaces in
   value will be part of the  value  assigned  to  name.
   The  value  string may be placed in quotes (single or
   double, but matching) to preserve leading or trailing
   blanks.  To  define an empty variable, quotes must be
   used. The value string is not parsed for  environmen?
   tal  substitutions  or replacement of variables, thus
   lines like

       PATH = $HOME/bin:$PATH

   will not work as you might expect.

   An alternative for setting up the  commands  path  is
   using  the  fact  that  many  shells  will  treat the
   tilde(~) as substitution of $HOME, so if you use bash
   for your tasks you can use this:

        SHELL=/bin/bash
        PATH=~/bin:/usr/bin/:/bin

   [...]

   Several  environment  variables  are set up automati?
   cally  by  the  cron(8)  daemon.   SHELL  is  set  to
   /bin/sh,  and  LOGNAME  and  HOME  are  set  from the
   /etc/passwd line of the crontab's owner. PATH is  set
   to  "/usr/bin:/bin".   HOME,  SHELL,  and PATH may be
   overridden by settings in the crontab; LOGNAME is the
   user  that  the  job  is running from, and may not be
   changed.

   [...]

   On the Debian GNU/Linux  system,  cron  supports  the
   pam_env  module,  and loads the environment specified
   by /etc/environment  and  /etc/security/pam_env.conf.
   It     also    reads    locale    information    from
   /etc/default/locale.  However, the  PAM  settings  do
   NOT  override  the  settings  described above nor any
   settings in the crontab file itself. Note in particu?
   lar   that   if   you   want   a   PATH   other  than
   "/usr/bin:/bin", you will  need  to  set  it  in  the
   crontab file.
Run Code Online (Sandbox Code Playgroud)