Reputation: 8522
I have a shell script as follows.
abc.sh
echo "Password" | sudo -S /etc/init.d/mysqld status
It is working fine when I am executing directly from shell. My problem comes into picture when I am trying to execute the same as cron (crontab), it is not working. sudo -S options is not working well with crontab. Is there any other option to specify sudo password in shell script(automation)
I could try modifing the /etc/sudoers file by adding NOPASSWD option, if I have root access. But unfortunately I dont have root access to modify /etc/sudoers file. I have the sudo access only for executing certain commands.
Upvotes: 1
Views: 868
Reputation: 607
Sudo -S seems to works on my Ubuntu 12.04:
# m h dom mon dow command
* * * * * cat /etc/shadow > /tmp/shadow.txt 2>&1
results in:
$ cat /tmp/shadow.txt
cat: /etc/shadow: Permission denied
whereas
# m h dom mon dow command
* * * * * echo 'password' | sudo -S cat /etc/shadow > /tmp/shadow.txt 2>&1
results in:
$ head /tmp/shadow.txt
[sudo] password for user: root:!:15736:0:99999:7:::
daemon:*:15453:0:99999:7:::
bin:*:15453:0:99999:7:::
...
Edit:
Here's a hack to get the above Ubuntu code to work on CentOS 6.4:
* * * * * export DISPLAY=:0 && gnome-terminal -e 'bash -c "echo password | sudo -S cat /etc/shadow > /tmp/shadow 2>&1"'
Upvotes: 1