Sh Script runs fine manually, fails in crontab - bash

I get this error while running thru crontab
/aws-cron-job/Ap_Hourly_xxxDelete.sh: 1: ./aws-cron-job/Ap_Hourly_xxxDelete.sh: ec2-describe-snapshots: not found
./aws-cron-job/Ap_Hourly_xxxDelete.sh: 1: ./aws-cron-job/Ap_Hourly_xxxDelete.sh: ec2-delete-snapshot: not found
This is my script: filename = xxx.sh
ec2-delete-snapshot --region ap-southeast-1 $(ec2-describe-snapshots --region ap-southeast-1 | sort -r -k 5 | grep "Ap_Hourly" | sed 1,4d | awk '{print $2};' | tr '\n' ' ')
This is my cronjob:
30 05-15 * * 1-6 ./aws-cron-job/Ap_Hourly_xxxDelete.sh > ./aws-cron-job/Ap_Hourly_xxxDelete.txt 2>&1
I can run this script manually but not through Cronjob. Where is the problem in this. Thanks in advance.

I believe that you should place only absolute paths in your cronjobs. As seen in your question, you wrote:
./aws-cron-job/Ap_Hourly_xxxDelete.sh
and I think you should write:
/<rootpath>/aws-cron-job/Ap_Hourly_xxxDelete.sh

The environment that commands run with as cron jobs is very limited, things like $PATH and $HOME are not what you'd expect.
To analyze this, use crontab -e to add the job * * * * * /bin/bash -c env >/tmp/cron.env, then look inside that file to see what bash knows about when started as a cron job on your machine. The job will run every minute, so when you're done debugging, remove it, also with crontab -e.
The error ec2-describe-snapshots: not found suggests that ec2-describe-snapshots might not be found in $PATH when the script runs as a cron job. To fix this, first find its normal location from the a shell with which ec2-describe-snapshots. Then, either use full path in script (/some/path/ec2-describe-snapshots ...), or adjust $PATH in script (PATH=/some/path:$PATH) before calling ec2-describe-snapshots.
Also, it's a good habit to use full paths in crontab entries, both for executables and for log files. However, the error in OP would not come from this.

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

Crontab can't loop

When I type the following in a terminal ./DHT 11 4 it works and saves all data to mysql correctly.
id (1), temp (29), hum (37), date (2015...)
When I add it to a crontab it does not work correctly.
id (1), temp (0 or empty), hum (0 or empty), date (2015...)
sh script:
#!/bin/bash
#DHT11
SCRIPT="/var/www/ErnestynoFailai/scripts/DHT 11 4"
#DHT22
#SCRIPT="/root/to/folder/DHT 22 4"
#AM2302
#SCRIPT="/root/to/folder/DHT 2302 4"
TEMP_AND_HUM=""
while [[ $TEMP_AND_HUM == "" ]]
do
TEMP_AND_HUM=`$SCRIPT | grep "Temp"`
done
TEMP=`echo "$TEMP_AND_HUM" | cut -c8-9`
HUM=`echo "$TEMP_AND_HUM" | cut -c21-22`
myqsl_user="root"
myqsl_pw="pw"
myqsl_database="DHT"
today=`date +"%Y-%m-%d %T"`
query="INSERT INTO DHT11 (temp, hum, date) VALUES ('$TEMP', '$HUM', '$today');"
mysql --user=$myqsl_user --password=$myqsl_pw $myqsl_database << EOF
$query
EOF
And crontab:
*/1 * * * * /var/www/ErnestynoFailai/scripts/write_DHT11_to_db.sh
What can be wrong?
Long time ago, it happened on some systems that cron didn't start shell scripts, only binaries. So you had to indicate explicitely which interpreter to use in the crontab line
*/1 * * * * /bin/bash /var/www/ErnestynoFailai/scripts/write_DHT11_to_db.sh
I didn't check since, and I dont know what system you are using. On debian/jessie, it is told in the crontab 5 manpage that the command is executed by /bin/sh, or the shell specified by the SHELL variable in the crontab file.
See https://superuser.com/questions/81262/how-to-execute-shell-script-via-crontab
Probably you have a problem of different environment settings. For debugging, an easy way is to include a line like the following to the beginning of your script:
set >/tmp/envlog.txt
Then compare its contents once created when you run your script directly and once using crontab.
Another way for debugging is:
exec >/tmp/scriptoutput.txt 2>&1
set -x
With this commands, the full output of your script will be redirected to the specified file.
Most often, the PATH variable is wrong. Instead of
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
You often only have a reduces version of it:
/usr/sbin:/usr/bin:/sbin:/bin
This means that some commands cannot be found. If you find a command which doesn't work, try finding out where it is located using:
$ which mysql
/usr/bin/mysql

Script not working with crontab but working when launching it normally

this is my first question on SO, otherwise I generally find what I need.
So here we go, here is my script :
#!/bin/bash
cd /home/laxa/Teeworlds_servers/scripts
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
kill `ps aux | grep -v "grep" | grep "/home/laxa/Teeworlds_servers/" | awk ' { print $2 } '`
and here is my crontab test :
48 23 * * * /home/laxa/Teeworlds_servers/scripts/restart_server.sh > /home/laxa/log.txt 2>&1
So, when I use the script in a bash shell with putty, the script gets executed fine and does it's job.
But when it's executed by the crontab, after some debug, it fails on the kill command.
I tried to set manually the PATH cause it was a big wrong.
Another friend told me to try to debug it, but apparently the script dies directly.
So I am quite blocked now, if someone has an idea or a solution, I would really welcome it.
Thanks.
Ok so, finally I founded that my line was working, but she was returning more processes than intended. And then I discovered that the script was killing himself, so thanks guys !

Running debian bash file as cron job

I can't seem to run a simple bash file as a cron job that runs once a minute.
#!/bin/bash
NET_INTERFACE=eth0
CURRENT_IP=`/sbin/ifconfig $NET_INTERFACE | sed -n "/inet addr:.*255.255.25[0-5].[0-9]/{s/.*inet addr://; s/ .*//; p}"`
wget -q --delete-after "http://abc.co.uk/raspiUpdate.php?pi=1&ip=${CURRENT_IP}"
In the crontab I have:
* * * * * /home/user/Scripts/script.sh
Where am I going wrong?
Thanks
To troubleshoot this cron issue a few steps come to mind:
double-check the scripts file name you put in your crontab:
$ ls -lah /home/user/Scripts/script.sh
$ cat /home/user/Scripts/script.sh
check that that script is executable by the user the crontab is installed for. i.e. if you use /etc/crontab, it should be executable by root, if you used
$ crontab -e
it should be executable by your userid.
Actually running that script should already give you a hint.
look for hints provided by cron. Depending on what distribution you run, the file cron logs to is different. For debian it should be /var/log/syslog IIRC
If you can't find an error message anywhere, it's hard to tell what's wrong. Generally if I reach that point, I stop and go to sleep or do something else.
[edit]
And when I return, i often find the error. :)
p.s.:
i recently had problems with dots in the filename. Some cron implementations ignore files in /etc/cron.* with dots in their filename. Documented here: http://manpages.ubuntu.com/manpages/xenial/en/man8/cron.8.html as Debian specific. But I think I had this issue also on Fedora.

Cron job terminates early

In my crontab file I execute a script like so (I edit the crontab using sudo crontab -e):
01 * * * * bash /etc/m/start.sh
The script runs some other scripts like so:
sudo bash -c "/etc/m/abc.sh --option=1" &
sleep 2
sudo bash -c "/etc/m/abc.sh --option=2" &
When cron runs the script start.sh, I do ps aux | grep abc.sh and I see the abc.sh script running.
After a couple of seconds, the script is no longer running, even though abc.sh should take hours to finish.
If I do sudo bash /etc/m/start.sh & from the command line, everything works fine (the abc.sh scripts run for hours in the background until they complete).
How do I debug this?
Is there something I'm doing that is preventing these scripts from running in the background until they are done?
The program(s) you're starting might be expecting a terminal to send their output to, or receive input from.
If you set the MAILTO= variable, and you have a sendmail(-like) daemon installed, you will get an email with the error message(s) it prints, if there are any:
MAILTO=your#email.address.here.com
01 * * * * bash /path/to/something.sh
Another way to debug would be to run the script from the command line, while redirecting all inputs and outputs:
$ sudo bash -c "foo.sh" > output_file 2>&1 < /dev/null
Also, the system log files (usually found in /var/log) might contain useful hints.

Resources