Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi! I'm running this script and it seems to "almost" work. It pings the IP address but when it gets a "1" condition it won't reboot like I want it to. Script:
ping -c 5 123.45.678.910
echo $?
if [ $? -ne 0 ] ; then shutdown -r -t 10 "now"
fi
That's it... I can run "shutdown -r -t 10 "now"" from the root and it works just fine.
The problem is that "echo" is treated as a command. It will print the exit code of the ping command, and then the exit code of the echo command is zero (0).
Something like this might work:
Code:
ping -c5 123.45.678.910
PING_EXIT_CODE=$?
if [ $PING_EXIT_CODE -ne 0 ]
then
echo $PING_EXIT_CODE
shutdown -r -t 10 now
fi
------ OR ------
ping -c4 123.45.678.910
if [ $? -ne 0 ]
then
echo $?
shutdown -r -t 10 now
fi
I do not know, for sure if the second example will give you the desired results, but the first one will, for sure.
PS. Do not post your question in more than one forum - the most appropriate place for this thread would be in the Programming forum.
Yes, the second example worked. I just tested it and will probably leave it like that.
Can you tell me what "echo $?" does in the 4th line?
Also, in the first example, is PING_EXIT_CODE=$? a standard command that's listed in some compendium of Linux commnands?
I've been going nuts trying to find insight on how all this comes together in some simple way but it's been really slow going.
This isn't a regular task for me at work but it would be nice to be able to get something together if I need to.
Thanks again for basically writing the code for me. When I started looking into this, I thought it would be a pretty easy task but it was a serious PITA until you saved the day.
hah! *blushes* Well, thank you - all in a day's work.
The "echo $?" in the fourth line means to print the value of the special variable "?", which is the exit code of the last command, to screen. Keep in mind, all references to variables in Bash are prefixed with a dollar-sign "$". It works in my example, because there is no command issued between the "ping" command and the "echo $?".
In the first example, "PING_EXIT_CODE=$?" is just a variable assignment - assign the exit code of the previous command (ping) to PING_EXIT_CODE. That way, if you wanted to issue a few more commands before testing the exit code of the ping command, you do not need to worry about the exit code of the ping command being "tainted".
Well now when I look at this stuff I see $? as the bash variable syntax. I had assumed that since a "1" was displayed from the root when pings failed that an echo had taken place and that the logical thing to happen would be for the script to see it and execute a shutdown. But really, I need to tell it to "echo" the $? (1 if ping fails) when -ne 0 and now that it sees a -ne 0 condition it will go ahead and do the shutdown according to the if/then lines.
it still makes my brain hurt. thanks again for the insight.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.