简体   繁体   English

如何使用非root用户设置cronjob?

[英]How to set cronjob with non-root user?

Error : crontab: must be suid to work properly 错误:crontab:必须suid才能正常工作

I want to set cronjob in docker container with non-root user. 我想在非root用户的docker容器中设置cronjob。 Is it possible to set a cron without using sudo and without installing cron? 是否可以在不使用sudo且不安装cron的情况下设置cron? I am using alpine docker container. 我正在使用高山码头集装箱。

Yes you can run its own, cron in specific user without sudo, As each user will be having the crontab file no need to install it explicitly. 是的,您可以在没有sudo的特定用户中运行自己的cron,因为每个用户都将拥有crontab文件,无需显式安装。 to run crontab of any user, use -u option , please note that user must be privileged to use -u 要运行任何用户的crontab,请使用-u选项,请注意用户必须有权使用-u

 $ crontab -u <username> -e

/etc/crontab is the system wide crontab . /etc/crontab是系统范围的crontab

The format of /etc/crontab is like this: /etc/crontab的格式如下:

# m h dom mon dow user      command
*   *  *   *   *  someuser  echo 'foo'

while crontab -e is per user, it's worth mentioning with no -u argument the crontab command goes to the current users crontab. 虽然crontab -e是每个用户,但值得一提的是,没有-u参数,crontab命令会转到当前用户crontab。 You can do crontab -e -u <username> to edit a specific users crontab . 您可以执行crontab -e -u <username>来编辑特定用户crontab

Notice in a per user crontab there is no 'user' field. 请注意,在每个user crontab中没有'user'字段。

# m h  dom mon dow  command
*   *   *   *   *   echo 'foo'

An aspect of crontabs that may be confusing is that root also has its own crontab. 可能令人困惑的crontabs的一个方面是root也有自己的crontab。 eg crontab -e -u root will not edit /etc/crontab 例如crontab -e -u root不会编辑/etc/crontab

See Configuring cron for more information: https://www.freebsd.org/doc/handbook/configtuning-cron.html 有关更多信息,请参阅配置cron: https//www.freebsd.org/doc/handbook/configtuning-cron.html

In most Linux distros, per user crontabs are typically stored in: /var/spool/cron/crontabs/<username> 在大多数Linux发行版中,每个用户的crontabs通常存储在: /var/spool/cron/crontabs/<username>

I solve it adding the name of the user we need to execute cron jobs in the file /etc/cron.allow. 我解决了它在文件/etc/cron.allow中添加执行cron作业所需的用户名。 If this files does not exists you have to create it. 如果此文件不存在,则必须创建它。 More details about cron.allow file 有关cron.allow文件的更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM