storing the variable value while run from cron - cron

sh
!/bin/sh
export count=$(sqlplus -s 'username/passwd#xyz'<
When I run this script manual , script runs fine.
When I keep it in crontab entry as follows
27 16 * * * /www/abc/a4.sh >a4.log
The a4.log doesnot have any data echoed.
Please let us know how to hold the value returned to a variable via sql query

Instead of putting
&>> your_file.log
behind a line in
crontab -e
Try using
#!/bin/bash
exec &>> your_file.log
…
at the beginning of a BASH script.
Also use >> which appends to the file rather than overwriting to it.

Related

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.

shell script - how to use variables for a shell script without defining the variable in the script

Raspbian (Jessy) - root#Raspberry Pi - Putty
In the Terminal i type in
finalanswer=0
now i got a script with this code
#!/bin/bash
source /lib/lsb/init-functions
echo $finalanswer #just as a test
if [ ! "$finalanswer" = "0" ]
then
rm -r mnt/objects/all
log_warning_msg "All Files has been deleted" || true
touch its_over.txt
else
let finalanswer=1
log_action_msg "Var finalanswer was 0. setting back to 1" || true
fi
there is a cronjob that starts this script every hour
sooo. somewhere there must be an error.
because he is reading the Variable $finalanswer as nothing.
that means variables that has been defined outside of this script will not work?
how do i fix this?
Shell variables are not inherited by child processes. If you want a variable to be inherited, it has to be an environment variable. You create environment variables using the export command.
export finalanswer=0
or
finalanswer=0
export finalanswer
You can also export a variable just for the duration of a command by putting the assignment at the beginning of the command:
finalanswer=0 /path/to/script
Note that variables you assign in your shell will not be accessible to cron jobs. Variables can only be exported to processes that are descended from the shell, and processes run by cron are not related to your shell process. If you want to set a variable for use in a cron job, you can put the assignment into the crontab file itself.
You can define a variable for a single command by placing its definition before the command you wish to run:
$ VARIABLE=hunter perl -E 'say $ENV{VARIABLE}'
hunter
you can do the same thing for a cron entry:
*/10 * * * * VARIABLE=hunter <command>

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.

array declaration of shellscript doesnot work in scheduled cron job?

I have created a shell script named "script.sh" which reads an arrays elements and prints it on terminal. The script is as follows:
arr=("hello" "world")
for i in ${arr[#]}
do
echo $i;
done
It gives expected output i.e 'Hello World' on executing it in terminal, but if I schedule the same script in crontab jobs to get executed automatically every minute and store the output in another file,the job fails and gives
/home/vikash/script.sh: 1: Syntax error: "(" unexpected error.
The crontab job to execute the script every minute and store the output in another file is as follows:
* * * * * $HOME/script.sh >> $HOME/output.log 2>&1
How to use array in this scenario?? please help.
Add this before the first line of your script:
#!/bin/bash
if this question is still valid, just run your script with
bash script.sh
I also tried it with sh somehow it is not working with sh but with bash it suns smooth.

Test run cron entry

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.

Resources