Bash script working fine but not as cronjob - Raspberry Pi - linux

I want to backup some audio files from my Raspberry Pi to a mounted network share (SMB). I've put together this script:
#!/bin/bash
dt=$(date '+%d-%m-%Y_%H-%M-%S');
sudo rsync -a -v /home/pi/davisbot/audio/ /mnt/resources/davisbot_audios/ --log-file=davislog.log
sudo cp -rv /home/pi/davislog.log /mnt/resources/davisbot_audios/"$dt"davislog.log
When I run the script, it works perfectly and instantly backups said files to the share. My cron job however doesnt work (Contents of crontab -e):
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/pi/davisbackup.sh
* * * * * /home/pi/davisbackup.sh
In the first line I've put the PATH variable because someone told me it might help, unfortunately it didn't. Also I've tried this:
* * * * * bash -lc /home/pi/davisbackup.sh
which also didn't help. I used sudo service cron restart during every try. What is left that I can try? Help would be greatly appreciated :)

Related

bash script doesn't work through crontab

I am running a bash script that transfers files to my AWS bucket.If i run the bash script through my terminal it works fine (via ./myBash.sh).
However I put it in my crontab but there it doesn't work.This is my bash script
#!/bin/bash
s3cmd put /home/anonymous/commLogs.txt s3://myBucket/
echo transfer completed
echo now listing files in the s3 bucket
s3cmd ls s3://myBucket/
echo check
And this is my crontab-
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
46 13 * * * /bin/bash myBash.sh
And here is a list of things i have aready tried -
1)tried running the crontab with a node app to test whether crontab was working(the answer was yes)
2)tried running the crontab without the SHELL and PATH
3)Tried running the bash script from cron using sudo (46 13 * * * sudo myBash.sh)
4)tried running the bash without the /bin/bash
5) Searched many sites on the net for an answer without satisfactory results
Can anyone help me with what the problem may be?(I am running Ubuntu 14.04)
After a long time getting the same error, I just did this :
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /bin/bash /home/joaovitordeon/Documentos/test.sh
For anyone coming to this post.
I was having same problem and reason was crontab was running under root user and s3cmd was configured under ubuntu user.
so need to copy .s3cfg to root
cp -i /home/ubuntu/.s3cfg /root/.s3cfg

SCP not working in crontab but works on commandline

After much research, I couldn't find a solution but post this question.
I have a computer A and B both Ubuntu desktop. I want to copy file from A to B. Steps I followed.
1. ssh-keygen in computer A
2. Left password blank
3. Copied id_rsa.pub to computer B ~/.ssh/ from computer A
4. Renamed id_rsa.pub to authorized_keys in computer B
5. In computer A I did scp -i ~/.ssh/id_rsa -r /var/www/abc abc#ip:/home/abc/
If I do step 4 in commandline its working fine. But when I did same in crontab
22 10 * * * root scp -i ~/.ssh/id_rsa -r /var/www/abc abc#ip:/home/abc
Its doing nothing.
I have tried virtually every answer found related to the problem. The answer just came accidentally.
I typed username instead of root and it worked. I don't know how but it worked. Hope this will help people like me.
2 10 * * * root /usr/bin/scp -i /home/username/.ssh/id_rsa -r /var/www/abc abc#ip:/home/abc
2 10 * * * username /usr/bin/scp -i /home/username/.ssh/id_rsa -r /var/www/abc abc#ip:/home/abc
This is my solution. Made in Raspberry with Jessie OS.
Fix connection with the server with public key with no passphrase. You can find tutorials everywhere.
The thing is do it as the same user that shall create the crontab.
In my case I made the keys as PI (user on my Raspberry). Make sure you can login without password on your server.
Then I created my script that uploads all txt-files in a directory to the server every 5th minute.
ex:
"#!/bin/bash
scp /mnt/www/hus/*.txt xxxxxx.se#ssh.xxxxx.se:/www/images/hustemp"
Save it as xxxxxxx.sh in your home dir and make it executable (chmod +x xxxxxxx.sh).
Then itś time to create the cronjob. I think you have to be in your home dir.Just run crontab -e (no sudo in front)and edit to what you want. In my case:
*/5 * * * * /home/pi/upload.sh
It works perfect!
Good Luck
Anders
Why don't you try putting the the scp command in a bash script and put the bash script in the cron , also remember to put the shebang in your sh script as follows : #! /bin/bash (generally the path , confirm this by typing which bash in your shell). Also chmod a+x your sh script to make it executable and try the sh script from bash as ./script.sh and then put it in the crontab.
Why did the scp command not work in crontab?
The following post does a good job explaining the different kinds of problems one faces with cron jobs -
https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work
In your case it's an environment problem. Crontab's environment is different from that of bash's.
Hope this helps.
In crontab, you have a mere execution of the command line without all the goodies of an interactive shell, that is a populated PATH variable, and all other bash tricks such as ~ interpretation (unsure for that last one).
So the rule is always use full paths in crontab:
22 10 * * * root /usr/bin/scp -i /home/username/.ssh/id_rsa -r /var/www/abc abc#ip:/home/abc
For those struggling with this issue, all the answers above didn't solve my problem. Actually, you have to do the scp once with the root account in the terminal, so that you get this message:
The authenticity of host 'XXXX (123.123.123.123)' can't be established.
ECDSA key fingerprint is SHA256:XXXXXXXXXXXxxxxXXXxxXXXxxXXxXxxXXX.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Then you type "yes" and your next crons will work like a charm.
Create a shell script entering the scp command in the root
Make the script executable
Put the script in crontab
PATH=/usr/bin
32 18 * * * cd /root/ ; (time ./infra.sh)
Step 5 doesn't work,maybe Step 3 and 4 doesn't work well.
3. Copied id_rsa.pub to computer B ~/.ssh/ from computer A
4. Renamed id_rsa.pub to authorized_keys in computer B
You should use the command "ssh-copy-id" to copy .pub file.

Run cron job hourly

I use let's encrypt for getting certificates and I want to setup renewal for certificates.
So, I decided to check if cron works fine.
I created three file in daily.hourly folder:
test-h:
/sbin/ifconfig >/home/bitnami/ipt
test-h2:
#!/bin/bash
/sbin/ifconfig > /home/bitnami/ipt2
test-h3.sh:
#!/bin/bash
/sbin/ifconfig >/home/bitnami/ipt3
But, I don't see my files in home directory. How to properly use cron.daily?
PS. The cron servive is started, I checked.
I restarted it also just to make sure that changes is applied.
The crontab file contains record for cron.hourly:
17 * * * * root cd / && run-parts --report /etc/cron.hourly
I am not linux guy, so, if it possible get me detailed answer please.
The problem is you didn't chmod +x your scripts. That's needed to make them executable.

Implement cron schedular in linux

I am trying to execute a php script in my linux server. the script will run everyday at 8 am. I have uploaded my crontab in linux server and the php file update1.php Script in my crontab is given below. But this isnt updating my databse. where am i doing wrong ? Am i missing something here. Thanks in advance.
crontab
0 8 * * * http://www.mywebsite.com/update1.php
You can use --spider option of wget for this purpose.
0 8 * * * wget --spider http://www.mywebsite.com/update1.php
--spider indicates not to download anything (we just want to go through the pages, that’s all)
This line/code in your crontab:
http://www.mywebsite.com/update1.php
is not an execution of the script, it's just the url to it. You should download the php script, and put it locally on your linux server. That would make things simpler and much more reliable.
I would login to your linux server, use wget to download the script, chmod it to make sure it's executable, and mv it to wherever you want on your server:
wget http://www.mywebsite.com/update1.php -O update1.php
chmod 755 update1.php
mv update1.php /path/to/where/you/want/script
Then now that you have the script locally on your linux server, edit your crontab (crontab -e), and add the following line:
0 8 * * * /path/to/where/you/want/script/update1.php

Active cron job

I am trying to make a cron job for the first time but i have some problems making it work.
Here is what i have done so far:
Linux commands:
crontab -e
My cronjob looks like this:
1 * * * * wget -qO /dev/null http://mySite/myController/myView
Now when i look in:
/var/spool/cron/crontabs/
I get the following output:
marc root
if i open the file root
i see my cronjob (the one above)
However it doesnt seem like it is running.
is there a way i can check if its running or make sure that it is running?
By default cron jobs do have a log file. It should be in /var/log/syslog (depends on your system). Vouch for it and you're done. Else you can simply append the output to a log file manually by
1 * * * * wget http://mySite/myController/myView >> ~/my_log_file.txt
and see what's your output. Notice I've changed removed the quiet parameter from wget command so that there is some output.

Resources