Linux - 切换到“cron”用户

Linux - 切换到“cron”用户

有时我会遇到 cron 作业无法运行的问题,诊断原因可能很困难。切换到 cron 作业运行所在的同一用户会很有用,这样我就可以查看是否存在权限问题或其他问题。有人能告诉我怎么做吗?我的 Linux 版本来自 ,uname -r2.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

(之所以有 42 13 这个时间是因为那是我最后一次测试它)

答案1

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

crontab -l

您可以在目录中找到用户特定的 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

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

/etc/crontab最后,您还可以通过运行来查看系统范围的 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

接下来是.您想要加载的文件。


最后,这里有一些相关信息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.

答案2

为什么不设置用户自己运行那个特定的 cronjob?例如:

42 13 * * * root cd /path/to/my/working/folder && ...

此作业将以 root 身份运行...如果您在 /etc/crontab 中输入作业

相关内容