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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-20-2013, 09:43 AM
|
#1
|
|
LQ Newbie
Registered: Feb 2013
Posts: 6
Rep: 
|
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.
|
|
|
|
02-20-2013, 04:24 PM
|
#2
|
|
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,624
|
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.
|
02-21-2013, 03:55 AM
|
#3
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,038
|
> 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.
|
02-22-2013, 03:36 AM
|
#5
|
|
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,624
|
Quote:
Originally Posted by adumith
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.
|
02-24-2013, 05:43 PM
|
#6
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,568
|
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 05:54 PM.
Reason: fixt link
|
|
|
1 members found this post helpful.
|
02-25-2013, 02:23 PM
|
#7
|
|
LQ Newbie
Registered: Feb 2013
Posts: 6
Original Poster
Rep: 
|
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.
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.
|
|
|
|
|
02-26-2013, 07:19 AM
|
#8
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,568
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:49 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|