[英]Running python cron script as non-root user
I have a small problem running a python script as a specific user account in my CentOS 6 box. 我在CentOS 6框中以特定的用户帐户运行python脚本时遇到一个小问题。
My cron.d/cronfile looks like this: 我的cron.d / cronfile看起来像这样:
5 17 * * * reports /usr/local/bin/report.py > /var/log/report.log 2>&1
The account reports
exists and all the files that are to be accessed by that script are chowned
and chgrped
to reports
. 帐户
reports
存在,并且该脚本要访问的所有文件都被chowned
并chgrped
为reports
。 The python script is chmod a+r
. python脚本是
chmod a+r
。 The python script starts with a #!/usr/bin/env python
. python脚本以
#!/usr/bin/env python
开头。
But this is not the problem. 但这不是问题。 The problem is that I see nothing in the logfile.
问题是我在日志文件中什么都看不到。 The python script doesn't even start to run!
python脚本甚至没有开始运行! Any ideas why this might be?
任何想法为什么会这样?
If I change the user to root
instead of reports
in the cronfile, it runs fine. 如果我将用户更改为
root
而不是cronfile中的reports
,则运行良好。 However I cannot run it as root
in production servers. 但是,我不能在生产服务器中以
root
身份运行它。
If you have any questions please ask :) 如果您有任何问题,请询问 :)
/e: If I do sudo -u reports python report.py
it works fine. / e:如果我执行
sudo -u reports python report.py
则工作正常。
Cron jobs run with the permissions of the user that the cron job was setup under. Cron作业使用设置Cron作业的用户的权限运行。 IE Whatever is in the cron table of the
reports
user, will be run as the reports
user. IE浏览器无论是在的cron表
reports
的用户,会随着运行reports
用户。
If you're having to so sudo
to get the script to run when logged in as reports
, then the script likely won't run as a cron job either. 如果您必须如此
sudo
才能使脚本作为reports
登录时运行,那么该脚本也可能不会作为cron作业运行。 Can you run this script when logged in as reports
without sudo
? 在没有
sudo
reports
下以reports
身份登录时可以运行此脚本吗? If not, then the cron job can't either. 如果不是,那么cron作业也不能。 Make sense?
说得通?
Check your logs - are you getting permissions errors? 检查您的日志-您是否遇到权限错误?
There are a myriad of reasons why your script would need certain privs, but an easy way to fix this is to set the cron job up under root
instead of reports
. 脚本需要某些特权的原因有很多,但是解决此问题的一种简单方法是将cron作业设置为
root
而不是reports
。 The longer way is to see what exactly is requiring elevated permissions and fix that. 更长的方法是查看究竟需要什么提升权限并加以解决。 Is it file permissions?
是文件权限吗? A protected command?
受保护的命令? Maybe adding
reports
to certain groups would allow you to run it under reports
instead of root
. 也许将
reports
添加到某些组将使您可以在reports
下而不是root
下运行它。
*be ULTRA careful if/when you setup cron jobs as root *如果/当您将cron作业设置为root时,请务必格外小心
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.