Want to schedule a sh-script but crontab does not execute it.
However, the script is executable manually without any issues.
"crontab -l"-Output:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
48 16 * * 1,2,3,4,5 /home/pentaho_admin/pdi/schedule.sh
the shell script included a command with the following command in the 2nd line:
./kitchen.sh
1st line:
alias xyz="cd..." (leads to the folder of kitchen.sh)
That first line apparently cannot be interpreted by crontab.
Solution:
I've added the full path to kitchen.sh
Is there any more sexy solution?
Related
I have a strange problem of being to able to run a bash script from commandline but not from the crontab entry for root. I am running Ubuntu 12.04.
* * * * 1-5 root /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh >> /home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/cron.log
If I run the script from the cmd line using bash, it works fine but sh fails with following error:
> jmeter-cron-randomise.sh: 7: jmeter-cron-randomise.sh: arithmetic
> expression: expecting primary: " % 1 "
Having googled the problem, it seems like standard shell doesn't have the same math operators, like % (modulus), as bash. I'm Not sure why the cron job is failing in the script? I am assuming it is because it's not using the bash shell? It's definitely being fired by the cron daemon (can see it in /var/log/syslog). Any help much appreciated.
You likely need to tell cron that the shell to use is the bash shell as it defaults to sh. You can do that for all crontab entries by putting this line in your crontab:
SHELL=/bin/bash
Note that this will cause all scripts in the crontab to be run under bash which may not be what you want. If you want to change the crontab line itself to just run bash, change it to this:
* * * * 1-5 root /bin/bash /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh >> /home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/cron.log 2>&1
Note that I have also caused stderr to be written to the cron.log file (2>&1) which may not be what you want but is pretty common practice. This may help you further diagnose errors from the script.
In case this helps anyone: for me this appeared to be because I had ended up with "DOS" line endings (CR-LF) instead of "unix" line endings (LF). This can be checked using od or your favourite hex dump tool, e.g.:
od -c <script_file>
... and look for \r\n instead of just \n.
It seems (and this article supports it) that the CR character stops the "shebang" from working because it's interpreted as part of the shell executable's filename.
(The line endings themselves appeared because the file came from a git repository and was transferred via a Windows machine).
I also encountered this problem trying to schedule a database backup as root and it made me pull my hair out! I was working on a CentOS 7 box.
Whenever I would check /var/spool/mail/root I would see a log:
sh: root: command not found, yet the command would run perfectly in the terminal.
This is what worked for me:
I created the crontab entry using crontab -e while logged in as root.
Using the command above as an example:
* * * * 1-5 root /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh >> /home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/cron.log
I deleted the root user entry like:
* * * * 1-5 /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh >> /home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/cron.log
That solved my problem.
I have the following test.sh file in /home/me folder
#!/bin/sh
_now=$(date +"%Y_%m_%d")
_file="/home/me/$_now.txt"
speedtest-cli --simple > $_file
Where speedtest-cli is a python script that gives internet up and dll speed infos : https://github.com/sivel/speedtest-cli.
Calling test.sh from /home/me works very good: I get my yyy_mm_dd.txt output with all infos (dll speed up speed, etc.).
But when I try to call the test.sh from a crontab I get a empty yyy_mm_dd.txt file (nothing inside).
Inside crontab-e
20 20 * * * /home/me/test.sh
Did I do something wrong?
I suspect a PATH problem, so
pick one of :
add PATH=/usr/local/bin:/bin:/usr/bin in the top of your script
add in the top of crontab -e : PATH=/usr/local/bin:/bin:/usr/bin on his own line
source ~/.bashrc in the top of your script
add full path to each commands in your script
Your PATH is probably different for your interactive shell than the context your cronjob runs in, so you should specify the full path of speedtest-cli in your crontab entry.
I have a script to backup my database at /home/<user>/bin/dbbackup. The script is executable by all users, and owned by me. The files /etc/cron.allow and /etc/cron.deny do not exist.
In my crontab I have the following lines (including a new blank line after the last line of code):
#reboot /home/<user>/.dropbox-dist/dropboxd
30 2 * * * bash /home/<user>/bin/dbbackup
However, cron is not running my dbbackup script. When I run a manual test of the script it works. When I run this test on the command line: * * * * * /bin/echo "cron works" >> ~/file I get the following error:
No command 'dbbackup' found, did you mean:
Command 'dvbackup' from package 'dvbackup' (universe)
Command 'tdbbackup' from package 'tdb-tools' (main)
dbbackup: command not found
My server is running Ubuntu Trusty. Any help please?
As the comments noted, it appears that amiga_os needed remove the reference to bash in the line.
30 2 * * * bash /home/<user>/bin/dbbackup
Should be.
30 2 * * * /home/<user>/bin/dbbackup
I usually just call scripts from their path and use "#!/bin/bash" (or wherever your bash lives) as the first line of the script. It appears the amiga_os had already done this, which is good. I don't like putting sentences into cron because it makes me nervous.
I think it was a path issue as cron executes as the user but does not read the bash profile and therefore does not work exactly like it would under your shell as it might not have access to your $PATH.
This is the content of my crontab -e file
#!/bin/bash
6 14 * * * /home/rishi/cront.sh
Also, the cront.sh file has only this
mkdir foo
I have been trying to make this work since the last 2 days. The cront.sh command works when ran from the terminal. But, does not work from crontab.
EDIT
It turned out that just editing the crontab -e using root
did the job. Nothing more had to be done.
Under cron, there is no guarantee that your environment variables (most importantly PATH) will be set proprerly.
Try adding line like this at the top of your crontab:
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
Also, it would be good idea to use full path for mkdir:
mkdir /path/to/my/dir
Also, it would not hurt to make sure that your cront.sh is executable:
chmod +x /home/rishi/cront.sh
After that, it should work.
EDIT
Generic method to debug crontab issues:
At the top of your script to debug, add a line:
set # this should print all environment variables
Execute your script manually, redirect output to some log file1.
Now, edit crontab to be something like this:
* * * * * /path/to/my/script 2>&1 > /path/to/log/file2
Be sure that log file will be writable for your script.
Also, be sure that your script has executable bit set.
Compare log file1 and log file2, paying close attention to env. variables. If they differ, use whatever method you want to set them to be the same. It could be adding lines to crontab, or using export var=value in your scripts.
After that, there is no reason for this to not work properly.
You don't need to first line
#!/bin/bash <--- remove this line
6 14 * * * /home/rishi/cront.sh
Is your script executable?
If it is not. Try running the following command in a terminal.
chmod +x /home/rishi/cront.sh
An example of crontab format with commented fields is as follows:
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * 0,6 /your/path/yourscript.sh
try
6 14 * * * sh /home/rishi/cront.sh
or
add first line in cront.sh
#!/bin/bash
or chmod a+x /home/rishi/cront.sh
My script is under /u01/software/aditya/script/ directory. Name of script is myscript.sh. I am able to run this script and getting output too. I am trying to set a cronjob for this script at 6.30 daily morning. I am doing this as root user. I have done following steps but not getting output.
crontab -e
30 06 * * * sh /u01/software/aditya/script/myscript.sh >> /u01/software/aditya/hello.log
:wq
but not getting any update in hello.log file :( . please help….
First check your cron log file which is usually in /var/log/syslog. There should be entries similar to
Sep 17 06:30:01 localhost CRON[17725]: (root) CMD (sh /u01/software/aditya/script/myscript.sh >> /u01/software/aditya/hello.log)
If not, your script has never been run. This could be due to a broken crontab file. You should make sure that this file always ends with a newline, better insert more than one at the end so that deleting one accidentally won't break the file.
If this line exists in the log file then your script has been run, but didn't generate any output. This can happen due to a different environment when being run via cron.
Also note that >> only redirects stdout, not stderr. If you want to redirect stderr too, then add 2>&1 at the end of the line.
Normally this is caused by a PATH problem. There is a very good chance that myscript.sh calls a command that is not available in the PATH that cron runs with. Some options to fix this are:
Make sure that every command in myscript.sh is a full path-reference (tedious)
Add source ~/.bashrc to the top of myscript.sh
Add export PATH=$PATH:<colon delimited list of paths necessary for myscript.sh to run correctly>
Pick one of the above, or you could also choose one of the options here: Hourly cron job did not run