简体   繁体   中英

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 Script:

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

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.

Note: I am using 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

#!/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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