开发者

Cron job in unix

开发者 https://www.devze.com 2023-01-18 22:48 出处:网络
i have made a script helloworld.sh and its path is /home/ehimkak/cronTabTest: #/usr/bin/sh echo $1 if [[ $1 = \"cron\" ]] ; then

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/t开发者_如何学Go1.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...


try to replace

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

with

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


something else for readability: instead of calling every 5minutes you can say it diffrent:

 bevore: 10,15,20,25,30,35,40,45,50,55 * * * * "/home/ehimkak/cro...
 after:  */5 * * * *  "/home/ehimkak/cro...

*/5 will call the cronjob every 5th minute, that makes it more readable! endo


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消