简体   繁体   English

在新贵脚本中更改用户

[英]changing user in upstart script

I have an upstart script that does some logging tasks. 我有一个执行一些日志记录任务的新贵脚本。 The script testjob.conf looks like below: 脚本testjob.conf如下所示:

description "Start Logging"
start on runlevel [2345]

script
  sudo -u user_name echo Test Job ran at  `date` >> /home/user_name/Desktop/jobs.log
end script

Then I run the script with sudo service testjob start and I get testjob stop/waiting as result. 然后我使用sudo service testjob start运行脚本,并且得到testjob stop/waiting作为结果。 The file jobs.log is created and the logging is done. 创建文件job.log并完成日志记录。 However the file is owned by root. 但是该文件归root拥有。 I wanted to change this and hence added sudo -u user_name part infront of the command mentioned in this similar post . 我想更改此设置,因此在此类似文章中提到的命令的前面添加了sudo -u user_name部分。

However this doesnot seem to do the trick. 但是,这似乎并不能解决问题。 Is there another way to do this ? 还有另一种方法吗?

The log file is created by the >> indirection which runs in the context of the root shell that also starts sudo . 日志文件由>>间接创建,该间接运行在也启动sudo的根shell的上下文中。

Try making the process that sudo starts create the file, for instance with: 尝试使sudo开始创建文件的过程,例如:

sudo -u user_name sh -c 'echo Test Job ran at  `date` >> /home/user_name/Desktop/jobs.log'

In this case the sh running as user_name will "execute" the >> indirection. 在这种情况下,以user_name身份运行的sh将“执行” >>间接寻址。

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

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