admin crontab won't run sudo command - cron

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

Related

"Syntax error near unexpected token `crontab'" when trying to add cron job in one line as sudo

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 -

How to set cronjob with non-root user?

Error : crontab: must be suid to work properly
I want to set cronjob in docker container with non-root user. Is it possible to set a cron without using sudo and without installing cron?
I am using alpine docker container.
Yes you can run its own, cron in specific user without sudo, As each user will be having the crontab file no need to install it explicitly.
to run crontab of any user, use -u option , please note that user must be privileged to use -u
$ crontab -u <username> -e
/etc/crontab is the system wide crontab.
The format of /etc/crontab is like this:
# m h dom mon dow user command
* * * * * someuser echo 'foo'
while crontab -e is per user, it's worth mentioning with no -u argument the crontab command goes to the current users crontab. You can do crontab -e -u <username> to edit a specific users crontab.
Notice in a per user crontab there is no 'user' field.
# m h dom mon dow command
* * * * * echo 'foo'
An aspect of crontabs that may be confusing is that root also has its own crontab. e.g. crontab -e -u root will not edit /etc/crontab
See Configuring cron for more information: https://www.freebsd.org/doc/handbook/configtuning-cron.html
In most Linux distros, per user crontabs are typically stored in: /var/spool/cron/crontabs/<username>
I solve it adding the name of the user we need to execute cron jobs in the file /etc/cron.allow. If this files does not exists you have to create it. More details about cron.allow file

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

crontab no working

I have crontab :
35 16 * * * mysqldump -h mysql2.alwaysdata.com -u user -ppass --all-databases > ../copias/fichero_`date +%d-%m-%Y-%H:%M:%S`.sql
but the command working correctly without crontab.
the folder chmod 777 -R.
Thanks.
You should use an absolute path instead of ../copias/fichero....
You don't know what the current directory will be when the command is run by cron.
In the /etc/crontab file you must specify the username as well before the command to run.

Resources