ansible task failed in crontab - ansible

My ansible task exec successfully in shell command line, but when I add it to crontab, it failed with error "No authentication methods available"
* * * * * root ansible all -i /tmp/host -f 10 -m ping 2&1 1>/dev/null
any ideas? thanks a lot.

Finally, I get this issue solved. It just works after "--private-key=/root/.ssh/id_rsa" added explicitly.
I haven't figure out why it's not essential in command line while on the other hand, it cannot be absent in crontab.
* * * * * root ansible all -i /tmp/host -f 10 -m ping --private-key=/root/.ssh/id_rsa 2&1 1>/dev/null

Related

How can I create a crontab in a Shell script

I have to automate my Crontab script and would like to insert some things in my Crontab file, I want to do that without interactive query and it should look like command:
crontab_admin.sh -add -s "15 9 * * *" -c "check_backup_include_list.sh" -u "USERNAME" -t "CRQ000000000000"
crontab_admin.sh -remove -s "15 9 * * *" -c "check_backup_include_list.sh" -u "USERNAME" -t "CRQ000000000000"
and it should look like this in crontab afterwards:
15 9 * * * $HOME/scripts/check_backup_include_list.sh
sry for my bad english
Its not wise to operate directly with cron file. But you can add record with script like this:
crontab -l >/tmp/c1
echo '15 9 * * * /full/path/to/scripts/check_backup_include_list.sh' >>/tmp/c1
crontab /tmp/c1
If you want to remove particular record you can do for example with something like this:
crontab -l >/tmp/c1
grep -v check_backup_include_list.sh' /tmp/c1 > /tmp/c2
crontab /tmp/c2

Cronjob not redirecting output

I have created one test.sh shell script which I have scheduled using crontab -e to execute after every 1 minutes and redirecting output to a file.
test.sh
echo "Printing all Environment Var"
env
echo "Bye Bye"
Below is how my crontab look like
#crontab
0,1,2,3,4,5,6,7,8,9,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59 * * * * sudo su - admiir -c /u01/users/admiir/test.sh > /u01/app/iir/InformaticaIR/iirlog/crontab_launchsh.log
When I run ls -ltr the timestamp is getting updated but nothing is getting printed in the output file.
To run the cron every minute and to save to a file the current environment used this could be used:
* * * * * env > ~/cronenv
Next, you can start a shell like it will be run within cron by doing:
env - `cat ~/cronenv` /bin/sh
Here you could try something like:
su - admiir -c "/u01/users/admiir/test.sh > /u01/app/iir/InformaticaIR/iirlog/crontab_launchsh.log"
You can omit the sudo su and only use su
Once your script is working you could then update your cron with:
* * * * * su - admiir -c "/path/to/test.sh > /path/to/out.txt"
You could also run the cron as the specific user by doing:
sudo crontab -u username -e

sh script works manually but not via cron

I'm trying to run a sh script from crontab. If I run script manually it works perfect, but when I run it in crontab, I get errors.
The script:
#!/bin/bash
sudo tar -zcvf /var/www/nextcloud/data/nextcloud/files/backup.tar.gz /home/beno/stuff/
sudo -u www-data /usr/bin/php /var/www/nextcloud/occ file:scan --all >> /var/www/nextcloud/data/nextcloud/files/backup_log.txt
The script is supposed to make a tar backup of a folder and put it in nextcloud folder and run command files:scan, so nextcloud rescans filesystem and starts synchronization...as I read it here:
https://doc.owncloud.org/server/9.0/admin_manual/configuration_server/occ_command.html#file-operations-label
When crontab runs the script, backup.tar.gz is created, then I get following error:
An unhandled exception has been thrown:
Doctrine\DBAL\DBALException: Failed to connect to the database: An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory in /var/www/nextcloud/lib/private/DB/Connection.php:60
I'm using ubuntu16 and nextcloud11. Please help!
Following you'r request
Instead of using you'r own crontab, you have to use specic user's crontab, by sudo your crontab command:
sudo -s
then
crontab -e
now you coul add the root's 1st line:
tar -zcvf /var/www/nextcloud/data/nextcloud/files/backup.tar.gz /home/beno/stuff/
and once finished
exit
sudo -s www-data
... and so on...
or (if you could'nt run a shell under www-data):
sudo www-data crontab <<<'01 2 * * * /usr/bin/php /var/www...'
Note: This will overwrite www-data's crontab by this only line!
To prevent this, you could:
sudo www-data crontab -l
to see what contain actual crontab, then
sudo www-data crontab -l |
sed -e '$a 01 2 * * * /usr/bin/php /var/www...'
For adding you'r line, and finally
sudo www-data crontab -l |
sed -e '$a 01 2 * * * /usr/bin/php /var/www...' |
sudo www-data crontab
For replacing actual crontab by modified one.
But it could be simplier to
sudo vi /etc/cron.d/mybackups
Then add you'r rule by following time spec by user name:
21 22 * * * root tar -zcvf /var/w...
22 23 * * * www-data /usr/bin/php /var/www...
Further doc
see man -P'less +/SYSTEM' 5 crontab

Crontab script su root / user -c does not execute

if been trouble-checking for hours and can't find out why my shell script won't execute properly when using a root crontab.
I'm on a vServer eqipped with
Ubuntu 14.04.4 LTS
3.13.0-042stab113.11.
my script is a chmod 711 file:
/usr/local/sbin/bckup_script
and looks like this:
#!/bin/bash
DATE=`date +%Y-%m-%d_%H_%M_%S`
su - -c "chgrp postgres /backup/db"
su - -c "chmod 770 /backup/db"
su - -c "chown user /backup/db"
su - postgres -c "pg_dump db_name > /backup/db/${DATE}db_name.sql && pg_dumpall > /backup/db/${DATE}_all_db.out"
su - -c "rsync -a /home/user/value /backup/"
The crontab is started using
crontab -e
as
root
user
The crontab executes as far as I can tell from syslog.
When executed as root user (no crontab), the file will do what it's told to. Also my PATH is set properly and working.
I have no idea what am doing wrong.
Solution:
Thx to Jay jargot I found out what was wrong. To complete the question, here are the outputs you "asked" for:
crontab -l
#m h dom mon dow command
* * * * * bckup_script
Output of crontab was
/bin/sh: bckup_script: command not found
which lead me to the conclusion to use the absolute Path to the file which solved the problem.
my crontab -l now looks like follows and everything works like a charm!
# m h dom mon dow command
49 20 * * 1-5 /usr/local/sbin/bckup_script
Thx very much!

How to start and stop a background script/cmd with crontab?

How can I start and then stop a tcpdump cmd/(or bash script) via crontab? Thanks
There is no difference to run tcpdump or any other command via crontab. You can create a script that stops tcpdump at the beginning and start it again. crobtab.sh
#!/bin/bash
killall tcpdump
var=$(date +%G%m%d-%k%M)
/usr/sbin/tcpdump -i eth0 -C 10 -w ~/$var.txt
Then run crontab for this script
00,15,30,45 * * * * /path_to_crontab.sh
That means this script will run every 15 minutes at everyday

Resources