简体   繁体   English

Cron Job未执行具有要以其他用户身份执行的命令的Shell脚本

[英]Cron Job not executing the Shell Script that has Command to execute as another user

I have a shell script which executes a command using another user 我有一个Shell脚本,可以使用另一个用户执行命令

Shell Script: Shell脚本:

su user1 -c 'command'

When I invoke this in the terminal its fine, it ask for Password and things go well. 当我在终端中调用它时,它要求输入密码,一切顺利。

I scheduled this using a cron job like this 我使用这样的Cron作业安排了这个时间

sudo crontab -e

*/5 * * * * /path/to/shellscript.sh

But the cronjob doesn't seems to execute the above command "su user1 -c 'command'", all the echo statements and loops executes. 但是cronjob似乎没有执行上面的命令“ su user1 -c'command'”,所有的echo语句和循环都执行了。

Note: I am using Mac OS X 注意:我使用的是Mac OS X

It need password. 需要密码。 You may need to keep password in a text file do as follows 您可能需要将密码保存在文本文件中,操作如下

echo password | su user1 -c 'command'

As you are running the script as another user, you need to supply the password in the shell script as 当您以其他用户身份运行脚本时,需要在shell脚本中提供以下密码:

#!/bin/sh
PASSWORD="pass123"
echo PASSWORD>su user1 -c 'command'

Then when you run the cron it shall pick up the command as that user, due to the presence of credentials. 然后,当您运行cron时,由于存在凭据,它将以该用户的身份接管该命令。

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

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