CRON chown and chgrp to a directory from root user [duplicate] - cron

This question already has an answer here:
CRON - chown and chgrp to a directory from root user to www-data
(1 answer)
Closed 9 years ago.
I am trying to change group and owner (from root to www-data)for a directory at each 5 min interval.
So I have set a cron on root user like `
0,5 * * * * sudo /bin/chown -R www-data /var/www/pdf/ && sudo /bin/chgrp -R www-data /var/www/pdf/
But it's not working .
Kindly help me out.
Thanks in advance.

0,5 * * * * sudo /bin/chown -R www-data /var/www/pdf/ && sudo /bin/chgrp -R www-data /var/www/pdf/
First off, the chgrp is redundant, you can manage the same with the chown command itself.
So instead of doing sudo /bin/chown -R www-data /var/www/pdf/ && sudo /bin/chgrp -R www-data /var/www/pdf/, you can do sudo /bin/chown -R www-data:www-data /var/www/pdf
Next instead of 0,5 * * * * as your cron frequency, run it using */5 * * * *
Finally, instead of adding cron to a user's crontab with sudo / to systemwide cron using /etc/cron.d, add it to the root user's crontab using
sudo crontab -e
*/5 * * * * /bin/chown -R www-data:www-data /var/www/pdf/

Related

How to: sudo -u <username> in sudoer?

I need to launch a command with sudo rights out of a php file (user: www-data), explicitly as user www-data:
<?php
$command = 'sudo -u www-data /usr/bin/python3 /var/www/html/script.py';
shell_exec($command);
?>
to be able to use sudo for www-data I want to put the command in sudoers (sudo visudo), like:
www-data ALL=NOPASSWD: sudo -u www-data /usr/bin/python3 /var/www/html/script.py
or
www-data ALL=NOPASSWD: -u www-data /usr/bin/python3 /var/www/html/script.py
but the syntax is wrong (error message from visudo).
The following is working with sudoers (correct syntax)
www-data ALL=NOPASSWD: /usr/bin/python3 /var/www/html/script.py
but doesn't work for my script (apache error in log file):
Sorry, user www-data is not allowed to execute '/usr/bin/python3 /var/www/html/script.py' as www-data on raspberrypi.
it seems it needs sudo -u www-data. How can I solve this?
It makes no sense to use sudo to allow www-data to run commands as www-data, but you can easily do so:
www-data ALL=(www-data) NOPASSWD: /usr/bin/python3 /var/www/html/script.py
The problem with your approaches was that you tried to add the command sudo -u www-data .. to sudoers, which corresponds to double-sudo sudo sudo -u www-data ..

admin crontab won't run sudo command

sudo: no tty present and no askpass program specified
Hi I am getting the above error when ever I try to get the following crontab to run.
*/5 * * * * sudo bash /home/admin/scripts/monitor.sh /dev/null 2>&1
I am using nano as the editor to edit the admin user crontab - note this is not root user.
EDITOR=nano crontab -e -u admin
1) Disable requiretty in sudoers file
2) Permit script execution without password:
admin ALL=(ALL) NOPASSWD: /home/admin/scripts/monitor.sh
3) I'm not sure but you don't need specify bash after sudo. Just add #! /bin/bash at the begin of the script
*/5 * * * * sudo /home/admin/scripts/monitor.sh /dev/null 2>&1

Script does not run under cron but runs manually again

I am very sorry for asking this again, but I've tried all advices. I have 2 scripts in /var/TPbackup_script/. This is the first one:
mysqldump -u root -pPASSWORD teampass > /var/TPbackups/TPbackup_$(date +"%Y-%m-%d").sql
Corresponding cronjob in /etc/crontab
20 9 * * * root sudo sh /var/TPbackup_script/TPbackup_script
This script works in crontab. All is good. The second script does not run:
s3cmd sync /var/TPbackups s3://PwdMgmt
Corresponding cronjob in /etc/crontab:
25 9 * * * root sudo sh /var/TPbackup_script/TPsyncS3_script
This one fails. If i run it manually in terminal:
sudo sh /var/TPbackup_script/TPsyncS3_script
then it works perfectly. What i tried:
1) Trying to add shebang #!/bin/sh to the beginning of the script
2) Renaming script to TPsyncS3_script.sh
3) I have added script into cron.daily and it was in the list of daily cron tasks (i see it with command run-parts --test /etc/cron.daily)
No success.
Here is my /etc/crontab file:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
16 9 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
20 9 * * * root sudo sh /var/TPbackup_script/TPbackup_script
25 9 * * * root sudo sh /var/TPbackup_script/TPsyncS3_script.sh > /var/TPbackup_script/sync_log.txt
#
All permissions on scripts were set with sudo chmod 777.
And by the way. sync_log.txt was created after cronjob, but it's empty.
Any help is appreciated
Had the same problem. Solved this by adding the option to specify the location of the s3cfg:
--config /root/.s3cfg
e.g:
s3cmd sync --config /root/.s3cfg /var/TPbackups s3://PwdMgmt
I had a similar problem. Try to run your script using root's crontab.
do:
sudo crontab -e
Add your script and try again. It worked for me :)

Why does these sudo commands fail? [duplicate]

This question already has answers here:
How do I use sudo to redirect output to a location I don't have permission to write to? [closed]
(15 answers)
Closed 9 years ago.
I would like to add a crontab entry from a script as a normal user, so I use sudo to get root permissions, but fails no matter what I try.
$ sudo { crontab -u root -l; echo ' 15 9 * * * root /opt/script.sh'; } | crontab -u root
bash: syntax error near unexpected token `}'
$ sudo echo ' 15 9 * * * root /opt/script.sh' >> /etc/crontab
bash: /etc/crontab: Permission denied
$ sudo echo ok
ok
Because you are running
sudo echo .......
as "su" then writing the result to /etc/crontab with:
>> /etc/crontab
so in the moment you are writing to /etc/crontab you're not "su" anymore
In sudo echo ' 15 9 * * * root /opt/script.sh' >> /etc/crontab, sudo echo ' 15 9 * * * root /opt/script.sh' is ran first then the shell takes the output of the sudo command and appends it to /etc/crontab. Since the shell is started as a normal user and so doesn't have root privileges, the shell can't write to /etc/crontab, which only root can modify. To solve the problem one starts a subshell as root, which allows it to append to /etc/crontab. Fortunately, this has already been implemented as su -c, however since the system uses sudo, sudo has to be prepended. The fixed command is sudo sh -c "echo ' 15 9 * * * root /opt/script.sh' >> /etc/crontab"

sudo password automation is not working as expected when executing from crontab?

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.
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"'

Resources