Test run cron entry - linux

I added a cron job recently, but made a mistake in the path while giving the command and hence, the job never succeeded. Is there some way to test the cron changes we have done?
Please note that I had indeed copied and pasted the command from my command line and it was just an stray keypress that caused this.

When I want to test my cron jobs I usually set the interval very low and monitor the logs closely. When I am convinced the entry is correct, I set the interval back to a sane value.
For example, run job every two minutes:
*/2 * * * * echo "Hello World"
And the I run tail -f on my log file (/var/log/syslogon debian).

This question has also been asked on serverfault and has garnered a couple additional answers
The following is a paraphrased version of Marco's solution:
(Not sure if best etiquette is not providing a link only answer or not copying someone else's solution)
Create a environment file with a temporary cron entry
* * * * * /usr/bin/env > /home/username/cron-env
Then create a shell script called run-as-cron which executes the command using that environment.
#!/bin/sh
. "$1"
exec /usr/bin/env -i "$SHELL" -c ". $1; $2"
Give it execute permission
chmod +x run-as-cron
and then it is then used like this:
./run-as-cron <cron-environment> <command>
e.g.
./run-as-cron /home/username/cron-env 'echo $PATH'

Joshua's answer does not work for me. Two problems:
Variables in cron-env file are not exported (set -a needed).
Script is still tied to current tty (setsid needed).
The script run-as-cron should be
#!/bin/sh
. "$1"
exec setsid /usr/bin/env -i "$SHELL" -c "set -a; . $1; $2" </dev/null
Not enough rep' to fix his answer or add a comment...

use command crontab -e
This will open a vim editor and all you got to do here is
* * * * * /somepath/urscript.sh , make sure you have the appropriate spaces between dates and the path of the script
After the execution , you can check in the /var/spool/mail there will a complete trail of the script execution or errors.
For testing there is no way .. but in case ur sh urscript.sh works then cron tab will have no problem as it is exactly same thing what u do manually.

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.

Trying to run script through crontab which won't work

I'm having issues getting my crontab to run I have the following line added to my crontab -e but it won't start. The command runs fine if I run it manually.
0 */3 * * * cd /home/sam/p/ && /usr/bin/python3.5 start.py
Not getting any error messages and can't see the process when I run top or grep for it.
Usually this happens because the cron environment is different from your own. Make sure your start.py script uses full paths to any referenced files or external scripts. Make sure that your start.py script does not rely on environment variables that you have in your shell but it may not. Try piping the cron output to a mail command so you can see what it is doing, like so:
0 */3 * * * cd /home/sam/p/ && /usr/bin/python3.5 start.py | mail -s "cron output" myself#example.com
An easier way to troubleshoot this is to write a wrapper shell script and send the output to a log file.
Create file python_start_cron.sh with contents
#!/bin/bash
cd /home/sam/p/ && /usr/bin/python3.5 start.py
Set the execute bit on this script script and make sure the script works manually
Modify the cronjob as shown below
0 */3 * * * python_start_cron.sh >/tmp/python_start_cron.log 2>&1
After cron executes, check the contents of the log file to ascertain the cause of the problem.

BASH shell script works properly at command prompt but doesn't work with crontab

Here is the script that I want to execute with crontab.
#!/bin/bash
# File of the path is /home/ksl7922/Memory_test/run_process.sh
# 'mlp' is the name of the process, and 'ksl7922' is my user account.
prgep mlp > /home/ksl7922/proc.txt
# This line will give the number of the process of 'mlp'
result=`sed -n '$=' /home/ksl7922/proc.txt`
echo "result = ${result}"
# if 'mlp' processes run less than six, than read text file one line and delete
# it, and execute this line.
if ((result < 6)); then
filename="/home/ksl7922/Memory_test/task_reserved.txt"
cat $filename | while read LINE
do
# Delete line first.
sed -i "$LINE/d" $filename
# Execute this line
eval $LINE
break;
done
else
echo "You're doing great."
fi
After that, I editted crontab and checked with crontab -l
*/20 * * * * sh /home/ksl7922/Memory_test/run_process.sh
This scripts works properly from command line, however, it doesn't work properly with crontab.
It seems like shell script works with crontab anyway, because 'proc.txt' file was generated, and the first line of 'task_reserved.txt' is removed.
However, I didn't see any messages, and result file of 'mlp' processes.
Since I'm not good at English, so I'm afraid that you guys don't understand my intention.
Anyway, can anyone let me know how to handle this?
My bet is the PATH environment variable is not correctly set within cron. Insert
echo $PATH > /tmp/cron-path.txt
to see what value it currently has. Perhaps you need to manually set it to a proper value within your script.
This is actually FAQ
https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work
https://askubuntu.com/questions/117978/script-doesnt-run-via-crontab-but-works-fine-standalone
If you don't have any mail installations on your system for cron to forward error messages from your script, it's a good practice to manually redirect all error messages to your preferred location. Eg.
#! /bin/bash
{
date
prgep mlp > /home/ksl7922/proc.txt
... snip ...
fi
} &> /tmp/cron-msg.txt
Have you checked the execute permission for the script? The file should have executable permission.
ls -ltr /home/ksl7922/Memory_test/run_process.sh
chmod 755 /home/ksl7922/Memory_test/run_process.sh

crontab failing to run a shell script that wgets a url at a specified interval

I have the following command inside a shell script at /home/ubuntu/wget_my_url.sh
the conent of ping_my_url are as follows -
wget -O - -q -t 1 http://www.someurl.com/baseurl/
There is no line break in the above file.
I did chmod +x wget_my_url.sh
Inside my /etc/crontab I have the following - */1 * * * * /home/ubuntu/wget_my_url.sh
There is a line break after the above line inside the crontab file.
When I run manually wget_my_url.sh, I get the desired result, but inside a cron tab, it does not run.
Please let me know, whats wrong with it.
Thanks.
What do you mean with the "desired result" ?
Your script just print choses on the screen (the standard out), but, the scripts on the crontab doesn't have a screen to print.
So, I think that it's running normally but you can't realize cause you don't see the script's output.
Instead of just print, why don't you print on a file making something like
wget -O - -q -t 1 http://www.someurl.com/baseurl/ > /home/ubuntu/OUT_FILE
?
Like that you'll be able to check if it works just checking if the file /home/ubuntu/OUT_FILE exists and you'll be able to use the script's output too.

Bash crontab doesn't output products of shell script file

Hi I'm a first year game programing student learning Unix Bash, I have run into a problem trying to understand crontab. I'm trying to do some rather simple things, checking to see if I am online, getting information about a given website, and ping another website to verify it is online. My script file does all of this without fail, however when I try to perform these tasks through crontab I get emails telling me absolutely nothing but jibberish. The output basically just tells me that I am trying to do all these things, but it doesn't output the results. I'm not sure where I am going wrong.
Just to verify I do have permission on the system to use crontab, and I have the script running every minute while I am trying to get it working. I'm hoping someone can point me in the right direction, all of my research online has really just led me astray.
This is my crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
HOME=/
LOGNAME=username
* * * * * /bin/bash /home/students/~/online.sh
30 23 1 * * rm online.log
this is my script
touch online.log
who | grep username >> online.log ; whois yahoo.ca >> online.log ; ping -c 1 www.google.com >> online.log
You need to use absolute paths in your scripts if you want to execute those using cron.
Note that cron executes in a different environment from what you get while executing a script on the command line. For example, changes lines like
touch online.log
to include the absolute path to online.log.
The output is being redirected into online.log, so you need to look there, not in your emails. If you want the output to be in the emails as well, you should look into using tee instead of a redirection.

Resources