Cron job sometimes fails with error by curl - bash

I made a cron job in (Ubuntu 14 Trusty), like this way:
sudo crontab-e
then
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
* * * * * curl --silent http://www....cron_script.php > /dev/null
But sometimes, I get this error message to the crown job:
/etc/cron.hourly/curl:
/etc/cron.hourly/curl: line 5: bin: command not found
run-parts: /etc/cron.hourly/curl exited with return code 127
Can anybody tell me why? As you can see, it runs every minute, but once a hour I get that error message. Mostly on every hour at XX:54 minutes.
Thanks in advance!

Your crontab should'n contain the line:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Since you've put the path in the crontab hourly script it executes every hour which make you error show up
Create a script instead and put both lines in there
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
curl --silent http://www....cron_script.php
then in crontab just run the script
* * * * * my_script.sh > /dev/null 2>&1
Crontab should only contains lines which follows the format:
m h dom mon dow command

I think it is not able to find command curl while running from crontab, so you need to do which curl it will give you location of it in your box. Then try to give exact path of that curl command eg--> /usr/bin/curl into cron entry and let me know if this helps you. Also your path details should be added to the user's DOT/BASH profile by which you are running the crontab, then it could work without giving complete path of curl command too.

Related

Shell script doesn't run properly while running from crontab

I read the other related topics but they didn't help me.
I have a shell script which checks if my python script is not running,it will run it. Otherwise it will just skip and do nothing.
It totally works when I use:
bash myshellscrip.sh
And I get the result that I want which is doing some tasks and sending emails to some correspondents. However, when I try to run this particular shell script on crontab, it doesn't send out the emails and doesn't do the other tasks.
I tried the following on crontab and none of them worked.
* * * * * /bin/bash /path/to/my/script/myshellscrip.sh
* * * * * /bin/bash /path/to/my/script/myshellscrip.sh >> /some/other/path/output.txt
When I save the changes into 'output.txt' file, it creates the file but it doesn't send the emails or doing other tasks.
I also tried the option of reboot because I need this program to run at start up too, and this didn't work:
#reboot /bin/bash /path/to/my/script/myshellscrip.sh
Does anyone know how to fix it?
EDIT:
As I was checking with the simplest shell scrip like:
#!/bin/sh
/usr/bin/python /home/pi/DCA/code.py
My crontab wouldn't have any output in my output.txt file although my code.py have something printing out, too.
However, when I use a very simple python code for example only a 'print' statement it will run and save the output into output.txt.
Seems like your shell script crashes / stops before it can do something (possibly due to the environment being different or permission issues). You can check /var/log/syslog to find out.
You could try removing /bin/bash, I don't think that's necessary?
Run the cron job in debug mode. for that, Add -x to the bash command on the cronjob and save their output in the file.
bash -x /path/to/script.sh >> /path/to/the/output.txt
You can find the problem.
Apparently crontab was running my script several times. So I tried to use different locking mechanisms to put a lock around my scrip but only using flock worked for me. In my crontab I added this line:
* * * * * /usr/bin/flock -n /tmp/ms.lockfile /bin/bash /path/to/my/script/myShellScript.sh

Output not going to file when ran as a cron job

I have the following bash script:
clean-tmp.sh
#!/bin/bash
tmpreaper 1h /tmp --test > ./tmpreaper.log
When I run it in the terminal using ./clean-tmp.sh, it writes to the file ./tmpreaper.log.
I added the script to the list of cron jobs using crontab -e:
*/5 * * * * cd /home/cron-jobs && ./clean-tmp.sh
I then checked cron's logs and this entry is in there every 5 minutes:
Feb 19 00:45:01 ip-172-31-23-184 CRON[1475]: (ubuntu) CMD (cd /home/cron-jobs && ./clean-tmp.sh)
But it's no longer writing to ./tmpreaper.log.
What on earth am I doing wrong?
Just specify an absolute path for your file, like tmpreaper 1h /tmp --test > /var/log/tmpreaper.log
#Kacy: Little difficult to say without cron logs, you could have a look to cron logs(/var/log/cron etc).
0,5,10,15,20,25,30,35,40,45,50,55 * * * * cd /home/cron-jobs; ./clean-tmp.sh
May be some systems wouldn't allow time period as the way you tried, try once in above way and let us know then.

crontab not working with shell script

i have one shell script which i want to run every hour.
My crontab looks like shown below
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
02 * * * * root run-parts /Weather/run.sh
but its not running every hour.
2 days before i had written same code in /etc/cron.d/0hourly file that time it was working perfectly but suddenly now it stopped working, then i tried writting it in crontab but its not running.
i checked the crontab running status it gave me following
crond (pid 4487) is running...
My log file at /var/log/root is showing like
Apr 9 06:02:01 sandbox crond[4487]: (*system*) RELOAD (/etc/crontab)
Apr 9 06:02:01 sandbox crond[4487]: (crontab) ORPHAN (no passwd entry)
Am i doing something wrong??
Is there any step i missed?
in which file should i write code either in /etc/crontab or /etc/cron.d/0hourly
Please help me resolve this.
Thanks in Advance
The leading 0, '02' is not necessary. I don't now if that will cause an error. There also seems to be a single tick at the end of your line. I like to explicitly set the shell with:
2 * * * * /bin/bash -l -c '/full/path/to/script >> /full/path/to/log.log 2>&1'

Cron job does not run

Following is the entry in the crontab:
MAILTO=abc#gmail.com
45 14 * * * /home/user/simple.sh
I've also done chmod +x on the simple.sh But the crontab does not run, it doesn't even send an email.
pgrep cron shows an Id. I also tried bouncing crond. But no luck!
Could someone please point out the mistake here
The simple.sh script is:
#! /bin/bash
echo hello
Thanks
Since you are doing a echo within the cron job script, you need to capture its output somewhere.
Your shebang and file mode (using chmod +x) are all right, so those aren't the issue here and running without /bin/sh should work fine.
Try using the following to see the output in cron.log file (This runs every minute)
* * * * * /home/user/simple.sh >> /home/user/cron.log
Note that cron jobs run in separate subprocess shell, with reduced environment, so its output won't be visible on your terminal.
Regarding sending of email - you need to have some mail package (like postman, mutt etc) configured for the cron daemon to send out error mails.
Do not use relative paths, but absolute ones. Also, indicate the binary running the script, that is /bin/sh (or whatever coming from which sh):
45 14 * * * /bin/sh /path/to/script/simple.sh
Maybe there shouldn't be a space in line 1 of your .sh script:
#! /bin/bash
to
#!/bin/bash
Although I could see why it would still seem to work from when invoked in an interactive shell (# could merely comment out the rest of the line).
Still, I'd guess at worst it'd merely ignore that line and inherit cron's interpreter of /bin/sh

Bash Script with Crontab

I'm trying to setup a crontab I have this in my current job in the current user I'm logged into
* * * * * /CS/day/get_info.sh
get_info.sh is supposed to output a text file every minute and I suspected that it would output a file in the same directory as the script is located but it doesn't.
I've also checked the syslogs to see if I could figure this out.
(user) CMD (/CS/day/get_info.sh)
(user) MAIL (mailed 46 bytes of output but got status 0x0001#012)
Can someone explain to me why this is happening?
Thanks
man cron tells you:
When executing commands, any output is mailed to the owner of the
crontab (or to the user named in the MAILTO environment variable in the
crontab, if such exists). The children copies of cron running these
processes have their name coerced to uppercase, as will be seen in the
syslog and ps output.
So you have to
cd into the appropriate directory yourself (cron will use $HOME)
redirect ANY output to a file of your choice
You can do both things in the crontab. But I recommend to do it in the first lines of the script itself:
#!/bin/bash
cd WHEREEVER_YOU_WANT
exec > YOUR_LOG_FILE 2&>1
The script is run in the home directory of the user and the file should be there as well. If you want it in the same directory as the script, either do a cd in your script or modify your crontab entry:
*/1 19-20 * * * cd /CS/day; /CS/day/get_info.sh
Another common problem with crontab entries is the environment. If the script works correctly in your terminal, try debugging it, when it is run from cron:
40 11 * * * bash -x /CS/day/get_info.sh >/tmp/get_info.sh.log 2>&1
Run it once only with current time, because otherwise you will overwrite your log file every minute.
On my case, I just had to install and configure an smtp client.

Resources