Crontab, can't execute all the script - shell

I need to run daily a shell script which contains the steps of the model Weather Research Forecasting. I used cron for this. The first executable of the script (ungrib.exe) works perfect, but the second executable "metgrid.exe" it's not working at all. When I run the script in the terminal of linux works perfectly.
This is an example (summarized) of my script:
#!/bin/bash
bash
./link_grib.csh /home/user/WRF/GFS/
./ungrib.exe
ln -s metgrid/METGRID.TBL.ARW ./METGRID.TBL
./metgrid.exe <- not running with crontab
The way that I configure my crontab is:
crontab -e
SHELL=/bin/sh
00 01 * * * /home/user/WRF/scripts/WRF_scripts.sh
Any idea?

I added in my crontab the full path of libraries related with the execution of WRF.
LD_LIBRARY_PATH=$LD_LIBRARY_PATH

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

How to write bash script that autoruns every 10 min?

I've been working on a project and I would so not like it to be taken by system crash.
So I wrote a script to backup my whole project directory into Dropbox.
But I had to run it every 10 min, if I could remember to do that.
Question: any way to auto-it-up?
Type the following command to enter cronjob:
$ crontab -e
To get crontab to run a task every 10 minutes you could type as follow:
*/10 * * * * /path_to_script
See additional read for it:
Wikipedia
cron-every-5-minutes
cron job every 5 minutes starting from a specific time
try this solution:
cat cronjob
*/10 * * * * sh /path/to/scipt.sh
Then:
chmod +x /path/to/scipt.sh
chmod +x cronjob
/etc/init.d/crond start #redhat based servers like centos
/etc/init.d/cron start #debian based servers like ubuntu
crontab cronjob
The obvious solution is by crontab, as others suggest. Here is another solution, by adding only one line to your backup-script.
If your system has installed and enabled the "at" suite, see:
man atd
man at
or atrun if you using bsd like system
you can simply queue the "next run-time" from your backup script. So, for example, if your backup script is called /home/joe/bin/copy_to_dropbox, add to the end of script the next line:
af -f /home/joe/bin/copy_to_dropbox now +10 minute
and run manually the backup script first time.
After the first backup, the at command in the script queues itself for the next execution.
You can check the at queue with the "atq" command.

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.

Writing a Cron Job That Can Access User Data

I'm trying to write a cron job that runs a report, and emails the result to an address defined in my user's ~/.bashrc file. I had this working perfectly on Fedora, but when I switched to Ubuntu, my solution no longer works. The command my cron job currently runs is:
. /home/myuser/.bashrc; /home/myuser/bin/runreport
If I run that command manually, or start it via Gnome-Schedule, it works perfectly, but it never seems to run. Is there something specific to Ubuntu that would be blocking this from running?
Output of crontab -l:
0 8 * * * . /home/myuser/.bashrc; /home/myuser/bin/runreport # JOB_ID_1
Output of grep -i cron /var/log/syslog:
Aug 4 08:00:00 localhost CRON[23234]: (myuser) CMD (. /home/myuser/.bashrc; /home/myuser/bin/runreport # JOB_ID_1)
If /home/myuser/bin/runreport is a script, add the following two lines to the top:
env
set -x
and change the crontab line to:
. /home/myuser/.bashrc ; /home/myuser/bin/runreport >/tmp/qq 2>&1
Then, when it runs, you should have all the environment variables, and the commands that were run, in the /tmp/qq file.
If it isn't a script, make a script that calls it and add the env line to it. That will at least give you the environment you're running in.

Resources