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"
Related
I am getting a "-bash: syntax error near unexpected token `crontab'" when I am attempting to write a cron job to crontab in one line. (This is for a launch configuration on EC2.) I am following this guide here.
This is my command:
sudo { crontab -l -u ec2-user; echo "* * * * * touch /home/ec2-user/dummy/dummy$ENV"; } | crontab -u ec2-user -
If I run without sudo, it tells me I "must be privileged to use -u." When I run with sudo I get "bash: syntax error near unexpected token `}'"
I am sure I have my syntax wrong with the sudo, but I am not sure where.
Another way to accomplish what you want:
sudo bash -c "{ crontab -l -u ec2-user; echo '* * * * * touch /home/ec2-user/dummy/dummy$ENV'; } | crontab -u ec2-user -"
This way, you only have to specify sudo once.
sudo can only run external commands, not shell syntax like brace groups (or loops/functions/if-statements/etc). Use it on the individual commands you need special privileges for:
{ sudo crontab -l -u ec2-user; echo "* * * * * touch /home/ec2-user/dummy/dummy$ENV"; } | sudo crontab -u ec2-user -
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
This question already has answers here:
Permission denied with bash.sh to run cron
(6 answers)
Closed 2 years ago.
I put a file.sh for crontab to execute and redirecting the output into an email to my mailox. The script works when I run it manually. Unfortunately, when crontab does the job, it has the following error (from the output sent to me):
/bin/sh: /home/zenoss/zen-remote-bkup.sh: Permission denied
My crontab setup is the following:
30 11 * * * /home/zenoss/zen-remote-bkup.sh 2>&1 | mail -s "Zenoss backup replication" email#abc.com
And these are the permission on the file.sh I need to execute:
-rw-rw-r-- 1 zenoss zenoss 1433 Nov 5 10:32 zen-remote-bkup.sh
[zenoss#server1 ~]$
Does anyone know which permission I am missing? Thank you.
You have to put the eXecutable bit on the script to do the job:
chmod +x /home/zenoss/zen-remote-bkup.sh
I am trying to archive my localhost's root folder with tar and want to automate it's execution on a daily basis with crontab. For this purpose, I created a 'backupfolder' in my personal folder. I am running on Ubuntu 12.04.
The execution of tar in the command line works fine without problems:
sudo tar -cvpzf backupfolder/localhost.tar.gz /var/www
However, when I schedule the command for a daily backup (let's say at 17.00) in sudo crontab -e, it is not executing, i.e. the backup does not update using the following command:
0 17 * * * sudo tar -cpzf backupfolder/localhost.tar.gz /var/www
I already tried the full path home/user/backupfolder/localhost.tar.gz without success.
var/log/syslog gives me the following output for the scheduled execution:
Feb 2 17:00:01 DESKTOP-PC CRON[12052]: (root) CMD (sudo tar -cpzfbackupfolder/localhost.tar.gz /var/www)
Feb 2 17:00:01 DESKTOP-PC CRON[12051]: (CRON) info (No MTA installed, discarding output)
/etc/crontab specifies the following path:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
I assume that crontab is not executing as this is a sudo command.
Is there a way how I can get this running? What is the recommended, safe way if I don't want to hardcode my root password?
Well, the command that works for you is
sudo tar -cvpzf backupfolder/localhost.tar.gz /var/www
Which means, you have to run the command with sudo access, and it will not work from within your crontab.
I would suggest adding the cron job to the root user's crontab.
Basically, do
sudo crontab -e
And add an entry there
0 17 * * * cd /home/user/backupfolder && tar -cpzf localhost.tar.gz /var/www
If that doesn't work, add the full path of tar (like /bin/tar).
Also, while debugging, set the cronjob to run every minute (* * * * *)
Basically the problem is the sudo command so we will allow sudo to run tar for the "user" without prompting for the password.
Add the following line in /etc/sudoers file.
user ALL=(ALL) NOPASSWD:/bin/tar
where user is the user installing the crontab.
I suspect a PATH problem, try to set some variables at the top of sudo crontab -e :
MAILTO=your_email#domain.tld # to get the output if there's errors
PATH=/usr/bin:/bin:/usr/local/bin:/usr/local/sbin:/sbin
You can write your command in a script like run.sh
#/bin/sh -l
tar -cvpzf backupfolder/localhost.tar.gz /var/www
then use the crontab to run the script.
IMPORTANT NOTE: the script's first line has the "-l" option.
Try it.
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"'