tags:

views:

52

answers:

3

i have made a script helloworld.sh and its path is /home/ehimkak/cronTabTest:

#/usr/bin/sh
echo $1
if [[ $1 = "cron" ]] ; then
  echo "hiiiii"
else
  echo "sorry"
fi

If I run it from the / location with command

/home/ehimkak/cronTabTest/helloworld.sh cron

it runs fine.


Now I added a cron job by first setting the editor as vi (export EDITOR=vi) and then used command crontab -e.

There I added a line

10,15,20,25,30,35,40,45,50,55 * * * * /home/ehimkak/cronTabTest/helloworld.sh cron>>/home/ehimkak/cronTabTest/t1.txt

The result is that the script is running, but the output is not as desired.

The output in the t1.txt file I get is

cron
sorry

but my output must come

cron 
hiiii

There is no problem in the script, but i don't understand why crontab is behaving in such a way.
Please help...

+2  A: 

try to replace

if [[ $1 = "cron" ]] ; then

with

if [[ "$1" = "cron" ]] ; then
Matthieu
sorry this didnt solved my problem
himanshu
+1  A: 

Form a typical crontab(5) manpage ....

The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. Percent-signs (%) in the command, unless escaped with backslash (), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

i would give you two advices:

  • first try to understand what is happening. Log is always your friend. Said that, in your script add something like:

    echo $# > /tmp/log

    echo $@ >> /tmp/log

    echo $* >> /tmp/log

And check if the parameters are being passed.

Another tip is try to pass everything escaped like:

10,15,20,25,30,35,40,45,50,55 * * * * "/home/ehimkak/cronTabTest/helloworld.sh cron" >>/home/ehimkak/cronTabTest/t1.txt
VP
A: 

i was able to solve the problem

and the solution was very simple

i just edited the shell line that is the first line now the first line is

#!/usr/bin/sh

so i added ! and my problem was solved

himanshu