linux - Why my sudo command in bash cannot be executed via crontab? -


here's simple runme.sh:

#!/bin/bash  /bin/echo 'cbeykfkt' | /usr/bin/sudo -s /bin/su -c "whoami;/etc/init.d/iptables stop" 

in which, 'cbeykfkt' password current user: samx, has root privilege (have appended "samx all=(all:all) all" in visudo). intend stop iptables @ specific time in crontab, nothing happened iptables service when time up. nevertheless, if execute bash runme.sh, works fine.

my crontab follows:

58 16 * * * /bin/bash /home/data/samx/runme.sh 2>&1 > /home/data/samx/log_cron 

nothing printed log_cron file. there wrong code? in advance.

p.s. error printed after moved 2>&1 end:

sudo: sorry, must have tty run sudo

does know what's mean?

authentification utilities sudo reading password controlling terminal (e.g. thru /dev/tty, see tty(4)), not standard input. (but pass -s sudo ask read password on stdin)

you use expect (which able deal terminals), configure /etc/sudoers disable password checking.

for example, have line like

%sudo   all=nopasswd:  

in /etc/sudoers file. allow member of sudo group use sudo without typing password.

this of course opens security hole in computer. @ own risk.

at last, carefully wrap script in setuid executable (write carefully such program in c, chmod u+s executable).


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -