Crontab absolute path not working - cron

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.

Related

bash script not working as expected when executed with cron [duplicate]

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.

Bash script runs manually in terminal but is not executed from crontab

I have a bash script that I want to be executed every 15 minutes, so I added this line to my crontab:
7,22,37,52 * * * * /path/to/my/script.sh
I've checked the directory path to be correct and the script runs correctly if I just run /path/to/my/script.sh manually from any directory. I have this bang line in my script:
#!/usr/bin/env bash
My script also references other scripts in the same directory as it, and I have run chmod +x on all scripts that are needed. I set the MAILTO to my email address and I was getting some Cron Daemon emails when I changed the line in my crontab to:
7,22,37,52 * * * * sh /path/to/my/script.sh
But I never received emails upon using
7,22,37,52 * * * * /path/to/my/script.sh
or
7,22,37,52 * * * * bash /path/to/my/script.sh
I made sure cron is running and I've also tried redirecting the output of my script to a log file, which is also only written in when I include the sh. However, if I run sh /path/to/my/script.sh from the home directory, it does not work. The only ways my script actually runs is if (from any directory) I call /path/to/my/script.sh or bash /path/to/my/script.sh. I'm pretty new to writing bash scripts so any help is very welcome.
#pvas The cron user environment should be treated with extra special care. The assumption that most users have is that they will have access to paths, directories, permissions etc. This is far from the case. Cron runs in a minimal environment and you must set up EVERYTHING - Paths, Permissions and the location where the scripts are running from.
1) I set up the environment myself.
2) I use fully expanded paths in my crontabs.
3) I make sure any directories that need to be read have read permissions.
4) I make sure that my password does not expire because that will block cron when it does.
5) Make sure underlying scripts are explicitly invoked (by Perl, Bash, Python whatever).
6) Pipe the command on the cron line to a LOG file (even better a log file with a TIMESTAMP).
Fix these things and then try again. Cron is particular, you need to set up everything.
For example:
#SETUP ENVIRONMENT
SHELL=/bin/bash
source /home/userfoo/.bash_profile
#RUN THE SCRIPT everyday at 11:50pm (23:50)
50 23 * * * userfoo /home/userfoo/script.sh >> LOGFILE.txt
<<
Crontab entries should have the following format
m h dom mon dow command
which confirms that your entry below
7,22,37,52 * * * * /path/to/my/script.sh
is correct. Having said that, you must close the crontab editor(:wq) for the changes to come to effect.
It is suggested you go through [ this ] cross site post which portrays the possible issues with cron jobs.
More about hashbang [ here ].

Running a crontab every 15 minutes not working on linux redhat

I want to run a crontab every 15minutes. I tried this:
0 */15 * * * ./usr/My_PATH/run.sh
But I get this error:
0 command not found
Is there something wrong with the syntax ?
Many thanks.
UPDATE:
I corrected the script and I tried this:
*/15 * * * * /My_Path/run.sh
and this
0,15,30,45 * * * * /My_Path/run.sh
In both cases I get an error.
#1 bash: */15: No such file or directory
#2 bash: 0,15,30,45 command not found
If this:
0 */15 * * * ./usr/My_PATH/run.sh
fails with this error:
0 command not found
then you're trying to run it as a shell command. You need to feed it to the crontab command. There are several ways to do this.
crontab -l will list the current contents of your crontab; it doesn't modify it.
crontab -e will open (a copy of) your crontab in a text editor and let you modify it. This is probably the simplest way to update it.
crontab filename reads the specified file and replaces your current crontab with its contents. (If you already have a crontab, this will quietly clobber it.)
The method I recommend is to keep a separate file containing your crontab (say, crontab.txt).
First, if you already have a non-empty crontab (check with crontab -l), save it to the file:
crontab -l > crontab.txt
Make whatever additions or other changes you want to that file, and then use
crontab crontab.txt
to install the updated crontab.
You can keep backup copies (I maintain mine in a source control system) so you can recover if you mess something up. And you can do a quick crontab -e if you want to test something, then re-run crontab crontab.txt to revert to the stored crontab.
The syntax of the crontab line in your question:
0 */15 * * * ./usr/My_PATH/run.sh
is correct, but the path ./usr/My_PATH/run.sh looks like it may be incorrect. Cron jobs run from your home directory, so the path is valid only if the usr directory is directly under your home directory (and in that case the ./ is unnecessary). It's probably better to specify the full path, which can start with $HOME/.
Yes.
First field is minutes. Second field is hours. You're setting it off at zero minutes past the hour, every 15th hour. So basically - 15:00 each day.
You want:
*/15 * * * * /some_script
Furthermore - ./ - it's a relative path, and that's probably a bad idea with cron, because it doesn't chdir to run stuff. Use an absolute path to avoid confusion. If you absolutely need to be in a particular directory for the script to work, you can try:
cd /path/to/script && ./this_script
So it's quite possible that you've got broken permissions or just not finding a relative path that you're using.

Linux system command not working in Perl script via crontab

I have added a script in crontab for every 30 minutes. The line goes as follows:
*/30 * * * * root perl /root/perl.pl
The above script has a execution of system command 'top' and it gets printed in a log file.
If I run it manually it runs fine. But while running it via crontab, it does not show up the desired results. Please can somebody help me with this. Thank you.
The command in the above perl script is:
$top = `sudo top`;
The error I am getting is:
sudo: sorry, you must have a tty to run sudo
I changed my command from sudo to visudo. But still the problem remains.
You should use the full path in cron (and use which perl to find the full path):
*/30 * * * * root /usr/bin/perl /root/perl.pl
OR better yet, make the script executable using chmod +x, and add the interpreter to the beginning of the script #!/usr/bin/perl -w and call it directly from cron
*/30 * * * * root /root/perl.pl
Also if there is a problem in the perl script, you could output the result from cron like this
*/30 * * * * root /root/perl.pl > /tmp/myscript.log
You should look into the requiretty setting with regard to visudo. Look for a line that reads Defaults requiretty. You could try commenting it out, but you will be sacrificing some security. See man sudoers.
You could also try running top in batch mode with one iteration:
$top = `sudo top -bn1`;
Batch mode option is for sending output to other programs.

Crontab no error but doesn't execute script

I'm trying to execute a shell script from cron on Freebsd.
To test whether crontab is working at all, I wrote the line
* * * * * echo "Hello" > /home/myuser/logile
and it work fine.
But when trying to execute any script it doesn't do anything, not even an error. (In the script I tried to run is just the same echo command)
Below is the output of crontab -l:
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
HOME=/home/myuser
MAILTO=myuser
* * * * * /home/myuser/shellscript.sh > /home/myuser/logfile
Why is the script not getting executed, although crontab is obviously running?
Permission for all files are set to rwxr-xr-x.
* * * * * /bin/sh /home/myuser/shellscript.sh
or
* * * * * /bin/bash /home/myuser/shellscript.sh
worked for me in Macosx 10.6 as rootuser
Have you checked that the command line has a linefeed/CR at the end of the line? I struggled for hours trying to find a reason for non-executing php script on cron when I simply hadn't pressed enter at the end of the line when I edited the cron jobs with crontab -e :-)
Have you checked /var/log/cron for clues?
Have you tried
* * * * * /bin/sh /home/myuser/shellscript.sh > /home/myuser/logfile
cron sends any errors via email to owner of the crontab file (often "root" so you might check that account's email). To have any errors mailed to "crontabOnFreebsd" put:
MAILTO=crontabOnFreebsd
in your crontab (near the top).
For more info issue this command:
man 5 crontab
If you are getting an error, then your logfile might not capture it, try this:
* * * * * /home/myuser/shellscript.sh > /home/myuser/logfile 2> /home/myuser/errorfile
Its been a while since I did any cron stuff; but things that always used to get me:
Environment variables not been set: generally I found it necessary to set up full paths (even to things like 'cat') to all commands [or at least set ENV variables within the script itself].
Ensure the user who owns the script etc is really the user which is running the script: this might not be the same user when you test from the interactive shell. Same goes for the directories/files that the script might write to.
Also: check the email for the root user - you might find that the errors have been diverted to the inbox, which may help you troubleshoot this further.

Resources