简体   繁体   English

如何 cron 需要 root 用户访问权限才能运行的 bash 脚本

[英]How to cron a bash script which requires root user access to run

I have a bash script that has a function written in Perl script called in the bash script.我有一个 bash 脚本,它有一个用 Perl 脚本编写的 function,在 bash 脚本中调用。 But when I try to run the script in cron the cron is not able to fetch the script as it says it requires root user access.但是当我尝试在 cron 中运行脚本时,cron 无法获取脚本,因为它说它需要 root 用户访问权限。 When I execute the script I switch to the data manager role to execute.当我执行脚本时,我切换到数据管理员角色来执行。 What can I do so that cron can also execute the script?我该怎么做才能让 cron 也可以执行脚本?

Few suggested converting the bash script into csh script also for reference asked me to add很少有人建议将 bash 脚本转换为 csh 脚本也供参考要求我添加

source /central/confg/global.cshrc

this is in the script but I am not good at shell scripting so I am having trouble converting bash script into csh script.这是在脚本中,但我不擅长 shell 脚本,所以我无法将 bash 脚本转换为 csh 脚本。 If anyone has any ideas what can be done here please share.如果有人有任何想法可以在这里做什么,请分享。

add the below line into file: /etc/sudoers将以下行添加到文件中: /etc/sudoers

$(USERNAME) ALL=(ALL:ALL) NOPASSWD: $(PATH TO SCRIPT)

The default user is root if no user is specified as a parameter when creating a cronjob.如果在创建 cronjob 时没有指定用户作为参数,则默认用户为 root。 Give the script the executable permissions to run successfully and to set any environment variables the root user needs to run the script.为脚本授予成功运行的可执行权限,并设置 root 用户运行脚本所需的任何环境变量。

Note: The root user should not be used for security reasons unless necessary.注意:除非必要,否则不应出于安全原因使用 root 用户。

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

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