LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-20-2013, 10:43 AM   #1
adumith
LQ Newbie
 
Registered: Feb 2013
Posts: 25

Rep: Reputation: Disabled
Question wget inside the sh script.


Greetings all,

It's my first message is this forum that some friends recommended me.

I'm starting with administering a linux server with Centos version and it's long way to go, because I do not control the operating system as much as I would like.

Right now I'm here with you because I have a problem with a sh script I've written, this one has within it several wget.

When I run the script from the command line works without problems but when I run it from crontab does not work.

I wonder if I can count on your support to tell me I'm doing wrong.

This is the script:
Code:
#!/bin/sh
cd /root/
wget http://www.midomain/phpscript.php?secc=32
wget http://www.midomain/phpscript.php?secc=19
wget http://www.midomain/phpscript.php?secc=10
wget http://www.midomain/phpscript.php?secc=29
wget http://www.midomain/phpscript.php?secc=24
wget http://www.midomain/phpscript.php?secc=20
wget http://www.midomain/phpscript.php?secc=26
wget http://www.midomain/phpscript.php?secc=27
wget http://www.midomain/phpscript.php?secc=13
wget http://www.midomain/phpscript.php?secc=37
wget http://www.midomain/phpscript.php?secc=23
wget http://www.midomain/phpscript.php?secc=83
wget http://www.midomain/phpscript.php?secc=34
(echo "Push was sucefull" && exit 0 ) || ( echo "Push failed" && exit 1)
Many thanks in advance.
 
Old 02-20-2013, 05:24 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
If you're running commands from crontab, you probably want to use the full command (e.g. /usr/bin/wget) as cron runs in a limited environment, so probably doesn't have the same $PATH variable as you do.
 
1 members found this post helpful.
Old 02-21-2013, 04:55 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,883

Rep: Reputation: 520Reputation: 520Reputation: 520Reputation: 520Reputation: 520Reputation: 520
> I wonder if I can count on your support to tell me I'm doing wrong.

We couldn't tell, but your computer can:

Code:
#!/bin/sh
exec >/tmp/cron.debug.$$ 2>&1
set -x
cd /root/
wget ...
Then check /tmp/cron.debug.* files.
 
1 members found this post helpful.
Old 02-21-2013, 01:50 PM   #4
adumith
LQ Newbie
 
Registered: Feb 2013
Posts: 25

Original Poster
Rep: Reputation: Disabled
Hello NevemTeve & Snark1994;

Thanks a lot for your reply, following yours recommendations at the end of the script would be:

#!/bin/sh
exec >/tmp/cron.debug.$$ 2>&1
set -x
cd /root/
/usr/bin/wget http://www.midomain/phpscript.php?secc=32
/usr/bin/wget http://www.midomain/phpscript.php?secc=19
/usr/bin/wget http://www.midomain/phpscript.php?secc=10
/usr/bin/wget http://www.midomain/phpscript.php?secc=29
/usr/bin/wget http://www.midomain/phpscript.php?secc=24
/usr/bin/wget http://www.midomain/phpscript.php?secc=20
/usr/bin/wget http://www.midomain/phpscript.php?secc=26
/usr/bin/wget http://www.midomain/phpscript.php?secc=27
/usr/bin/wget http://www.midomain/phpscript.php?secc=13
/usr/bin/wget http://www.midomain/phpscript.php?secc=37
/usr/bin/wget http://www.midomain/phpscript.php?secc=23
/usr/bin/wget http://www.midomain/phpscript.php?secc=83
/usr/bin/wget http://www.midomain/phpscript.php?secc=34
(echo "Push was sucefull" && exit 0 ) || ( echo "Push failed" && exit 1)

Please excuse me for my ignorance and lack of knowledge about it, because it shows that it is essential for you.

I will implement the script and tomorrow I will comment if it worked.

thanks a lot again.
 
Old 02-22-2013, 04:36 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Quote:
Originally Posted by adumith View Post
I will implement the script and tomorrow I will comment if it worked.
Note that we would be surprised if it now worked - NevemTeve's suggestion will just print a lot of debugging information to the files /tmp/cron.debug.<some number here>, which is the information ey wants you to post back
 
1 members found this post helpful.
Old 02-24-2013, 06:43 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


Does this have to be a POSIX-compliant /bin/sh script, or can you use bash instead? If so, it can be re-written to be much shorter and cleaner.

Code:
#!/bin/bash

exec >/tmp/cron.debug.$$ 2>&1
cd /root/

urlprefix='http://www.midomain/phpscript.php?secc='
urls=( "$urlprefix"{32,19,10,29,24,20,26,27,13,37,23,83,34} )

if /usr/bin/wget "${urls[@]}"; then
    echo "Push was successful"
    exit 0
else
    echo "Push failed"
    exit 1
fi
Since wget can download multiple urls at once, we can avoid all those unnecessary duplicate processes and connections.

arrays
brace expansion

You could also skip the array and use the brace expansion directly in wget, but I think it's cleaner to pre-prepare the url list before downloading.


Note too that the original final line was incorrect. Since it never tested the outcome of wget, the script would always evaluate as successful. Not to mention that it would only be checking the result of the final instance anyway.


If the code has to be posix, I suggest this instead:
Code:
#!/bin/bash

exec >/tmp/cron.debug.$$ 2>&1
cd /root/

/usr/bin/wget $( printf "$urlprefix%s " 32 19 10 29 24 20 26 27 13 37 23 83 34 )

if [ $? -eq 0 ]; then
    echo "Push was successful"
    exit 0
else
    echo "Push failed"
    exit 1
fi
printf
command substitution

Posix can be bit trickier when it comes to creating lists of things to operate on. Since it doesn't have arrays you have to depend on shell word splitting, and that's often difficult to do safely. But that's not a big worry here, as the urls don't have whitespace in them.


Finally, you may also want to consider using the mktemp command instead of relying on '$$' for the tempfile.

Last edited by David the H.; 02-24-2013 at 06:54 PM. Reason: fixt link
 
1 members found this post helpful.
Old 02-25-2013, 03:23 PM   #7
adumith
LQ Newbie
 
Registered: Feb 2013
Posts: 25

Original Poster
Rep: Reputation: Disabled
Hello David

Your reply is incredible ...!

Many thanks for the enormous contribution, but (and I apologize if you abuse your generosity) the link will now contain a setting that I have to generate in the script, this parameter is the MD5 of the date but the value of a constant.

Example:
Constan:
XYZ=´qwerty01´
parameter= md5(date().XYZ)

Can you tell me how I can do this you are asking me?

Beforehand I am enormously grateful for your help.

This can be done or I´m totally crazy....?

Thanks again for your reply.

Quote:
Originally Posted by David the H. View Post
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


Does this have to be a POSIX-compliant /bin/sh script, or can you use bash instead? If so, it can be re-written to be much shorter and cleaner.

Code:
#!/bin/bash

exec >/tmp/cron.debug.$$ 2>&1
cd /root/

urlprefix='http://www.midomain/phpscript.php?secc='
urls=( "$urlprefix"{32,19,10,29,24,20,26,27,13,37,23,83,34} )

if /usr/bin/wget "${urls[@]}"; then
    echo "Push was successful"
    exit 0
else
    echo "Push failed"
    exit 1
fi
Since wget can download multiple urls at once, we can avoid all those unnecessary duplicate processes and connections.

arrays
brace expansion

You could also skip the array and use the brace expansion directly in wget, but I think it's cleaner to pre-prepare the url list before downloading.


Note too that the original final line was incorrect. Since it never tested the outcome of wget, the script would always evaluate as successful. Not to mention that it would only be checking the result of the final instance anyway.


If the code has to be posix, I suggest this instead:
Code:
#!/bin/bash

exec >/tmp/cron.debug.$$ 2>&1
cd /root/

/usr/bin/wget $( printf "$urlprefix%s " 32 19 10 29 24 20 26 27 13 37 23 83 34 )

if [ $? -eq 0 ]; then
    echo "Push was successful"
    exit 0
else
    echo "Push failed"
    exit 1
fi
printf
command substitution

Posix can be bit trickier when it comes to creating lists of things to operate on. Since it doesn't have arrays you have to depend on shell word splitting, and that's often difficult to do safely. But that's not a big worry here, as the urls don't have whitespace in them.


Finally, you may also want to consider using the mktemp command instead of relying on '$$' for the tempfile.
 
Old 02-26-2013, 08:19 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Sorry, I'm afraid I don't quite understand your new requirement. Where exactly does the extra parameter need to go? Do you mean that each url has to contain a different string? If so, you'll need to load the array with a loop instead.

Please post a detailed example of the new data strings, exactly how they need to be generated, and how they need to be used.


And as I said before, please use [code][/code] tags around all of your code and data.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
executing perl script inside shell script scriptme101 Programming 3 04-11-2012 10:30 AM
Runs Multiple shell script inside a main script using crontab srimal Linux - Newbie 4 10-22-2009 07:19 PM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 03:40 PM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 10:10 PM


All times are GMT -5. The time now is 10:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration